I have a url api.domain.com that I am trying to redirect to domain.com/api BUT I want to preserve the look that it's still api.domain.com
I have tried the following
server {
listen 80;
server_name api.domain.com;
location ~ ^/ {
rewrite ^/(.*) https://domain.com/api/$1 break;
}
}
But when I hit an endpoint eg api.domain.com/user/1 in the browser it displays as domain.com/api/user/1.
Is it possible to get the domain to visually stay as api.domain.com/user/1
I see that you want to serve api.domain.com from domain.com/api. You just need to define the root parameter accordingly.
Assuming the root directive for domain.com is /server/path/to/domain.com/html, in your case, it will become /server/path/to/domain.com/html/api.
server {
listen 80;
server_name api.domain.com;
root /server/path/to/domain.com/html/api;
location / {
# Whatever you want to do
}
}
Related
I have an nginx configuration file which has two server blocks as below
server {
listen 80;
server_name example.net;
root /var/www/html;
charset utf-8;
location / {
# main domain, servers also example.net/query
}
}
server {
listen 80;
server_name *.example.net;
location / {
# wildcard subdomain
}
location /query {
# rewrite ^/query (.*)$ https://example.net/query$1 redirect;
# return 301 https://example.net$request_uri;
# here, I want to use configuration of server block 1
}
}
Is there any way to make location /query in server block 2 use the configuration of server block 1?
I have tried rewrite and return 301, basically they redirect *.example.net/query to example.net/query, but what I want is to allow *.example.net/query to work just like example.net/query without redirect.
How can I make nginx redirect all the requests to my subdomain to a folder?
Example:
http://sub2.sub1.domain.com/
that should indicate that sub2 is a folder in sub1.domain.com/sub2
How can I do this?
The main objective is to hide the folder to the user. So, it should continue as
http://sub2.sub1.domain.com/
My wish is to use a wildcard in sub2.
UPDATE:
I've tried:
server {
listen 80;
listen [::]:80;
server_name ~^(.*)\.sis\..*$;
location / {
proxy_pass http://sis.mydomain.com/$1$request_uri;
}
}
but it also didn't work, any error?
In the nginx directives for sub2.sub1.domain.com you'd put:
server {
listen 80;
server_name sub2.sub1.domain.com;
location / {
proxy_pass https://sub1.domain.com/sub2;
}
}
So any request going to sub2.sub1.domain.com gets proxied to → sub1.domain.com/sub2 (while masked as sub2.sub1.domain.com); no need for a redirect or rewrite this way either.
Wildcard Method
server {
listen 80;
server_name ~^(.*)\.sub1\.domain\.com;
location / {
proxy_pass https://sub1.domain.com/$1;
}
}
*the wildcard method above is untested.
I'm trying to use subdomains as query parameter on the domain itself.
An example would be the following:
I want nginx to take ab.example.com and call example.com?key=ab, now the backend will return a specific config, which should be used for the subdomain "ab".
Afterwards the user should see the content (logo branding to be precise) of example.com?key=ab but in the client's URL field the ab.example.com should persist.
And all further requests should show for example ab.example.com/login instead of example.com/login.
I hope that what I have said is sufficiently understandable. I have tried various examples from the internet and tried to find some hints.
The nginx file looks like:
server {
listen 80;
listen [::]:80;
server_name www.example.com *.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name www.example.com *.example.com;
ssl_certificate /path/to/certs/ssl.crt;
ssl_certificate_key /path/to/keys/ssl.key;
root /var/www/example_site;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ /index.html =404;
error_page 404 =200;
}
}
I have already tried to map, but it redirects to a wrong domain:
map $host $subdomain {
~^(?<sub>.+)\.example\.com$ $sub;
}
And tried adding a static if statement in the server block, too:
if ($host = "ab.example.com") {
rewrite . ?key=ab;
}
An additional server block did not help either:
server {
listen 80;
listen [::]:80;
server_name www.ab.example.come ab.example.com;
rewrite ^ https://example.com/?key=ab permanent;
}
Does anyone see what I am doing wrong or what part of the documentation I should read again?
You just need to do it inside your own server_name directive. You can assign a variable in a regexp directly there. If you need a different behavior for www. subdomain just remove *.example.com from the block and add this one in another file:
server {
listen 80;
server_name ~^(?<subdomain>.+)\.example\.com$;
return 301 http://example.com$request_uri?key=$subdomain;
}
Note that I didn't use rewrite, which you shouldn't need. Using return performs better. 301 stands for the kind of redirect. And then, you use your server_name assigned variable to redirect where you need.
I have looked at this question which describes how to redirect a url in nginx, as shown below
# main server block for www.test.com
server {
listen 80;
server_name www.test.com;
...
}
# redirect test.com to www.test.com
server {
server_name test.com;
return 301 $scheme://www.test.com$request_uri;
}
What I need to do is redirect a set of individual pages, so was wondering how to do this
e.g. test.com\index , test.com\home , test.com\main to test.com\index.php
Then I have some other pages to simply redirect simply to the .php extension
e.g. test.com\about to \test.com\about.php
e.g. test.com\contact to \test.com\contact.php
What is the best way to do this?
Found the answer... assuming the following server block for test.com
server {
listen 80;
server_name www.test.com;
...
}
Add the appropriate regex location path, and rewrite or return to the redirect url.
for test.com\index , test.com\home , test.com\main to test.com\index.php
location ~ ^/(index|home|main) {
rewrite ^/.* http://$server_name/index.php permanent;
}
for test.com\about to \test.com\about.php
location /about {
rewrite ^/.* http://$server_name/about.php permanent;
}
I have been tasked with a couple project.
We have two directories on our server, one is
http://example.com/app
and the other is
http://example.com/fw
What I have been asked to do, is redirect from http to https if any visitor lands on a page in these two directories (app and fw)
Here is what I have done so far in the config file. When I added the lines to my config file below 'server' and restarted the site would not come back up. Unfortunately I don't have access to the log files. Appreciate anyone willing to take a look at this
location ~ ^/miner/memclub/.+\.html$ {
rewrite ^(.+)\.html$ /bootstrap.php?file=$1.html last;
rewrite ^(.+)\.phtml$ /bootstrap.php?file=$1.phtml last;
error_page 404 = /404.php;
}
server {
server_name site.org;
server_name *.site.org;
location /app {
if ( $scheme = http ) {
rewrite ^ https://site.org/app last;
}
}
}
First of all I don't think you can have 2 server_name, merge those two lines into one line
server_name example.com *.example.com;
And to do the https redirect i would recommend using 2 separate servers, you need one listening to port 443 anyway
server {
server_name example.com www.example.com; # which ever you are using
listen 443 ssl;
location / {
# all your https configuration
}
}
server {
server_name example.com www.example.com:
listen 80;
location /app {
return 301 https://$http_host$request_uri;
}
location /fw {
return 301 https://$http_host$request_uri;
}
location / {
# the rest of the non https configuration
}
}
I know you can merge both app and fw into one location, but I believe doing it without regex is faster, if you want to do it anyways here it is
location /(app|fw) {
return 301 https://$http_host$request_uri;
}