There's a few similar questions on SO, but none exactly mine, and I've had no luck trying to adapt their answers so far.
I want to map the URL http://sub.example.com to https://123.12.12.12/path, such that the browser still shows the URL http://sub.example.com.
My Nginx config file looks like,
server {
listen 80;
server_name sub.example.com;
location / {
proxy_pass https://123.12.12.12;
rewrite ^/$ /path last;
}
}
The routing works here, but the URL displayed is http://sub.example.com/path. How do I make it display only http://sub.example.com?
server {
listen 80;
server_name sub.example.com;
location / {
proxy_pass https://123.12.12.12/path;
}
}
Thats how it works. If proxy_pass contains locations part - current location will be replaced to specified. http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
But it's help only for http request and http redirects. If application create html with links https://123.12.12.12 - it's still unchanged. In this case you can try ngx_http_sub_module.
I did like this:
server {
listen 80;
listen [::]:80;
listen 443 http2 ssl;
listen [::]:443 http2 ssl;
server_name domain1;
if ($request_method ~* OPTIONS|GET|HEAD) {
return 301 https://domain2$request_uri;
}
location ~* api {
proxy_pass https://domain2$request_uri;
}
}
Because post-requests will cause a 405 error when redirecting.
Related
In my nginx server , I want to do this:
mydomain.com/api/xxxx
redirect to mynewdomain.com/api/testing/xxxx
I try to do by different ways but no ones works for me, always return a 502 error;
First way
server {
listen 80;
server_name mydomain.com;
# Nginx conf
location /api/ {
return 301 https://mynewdomain.com/api/testing/$uri;
}
}
Second way
server {
listen 80;
server_name mydomain.com;
# Nginx conf
location /api/(.*) {
proxy_pass https://mynewdomain.com/api/testing/$args_v;
}
}
And I tryed to only redirect to specific direction and this works, but i don't want to hardcode all petitions to redirect,
Static redirect
server {
listen 80;
server_name mydomain.com;
# Nginx conf
location /api/ {
proxy_pass https://mynewdomain.com/api/testing/redirect1;
}
Any help for make this dynamic?
location ~ ^/api/(.*) {
return 301 https://mynewdomain.com/api/testing/$1;
}
Try this, because $uri also includes /api.
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 want to redirect to https when scheme is http and location is /
server {
listen 80;
server_name bla;
location / {
return 301 https://bla;
}
include fs.inc;
}
Now, problem is: fs.inc includes anothes "location /" and even if this is never called the nginx configuration test fails with error duplicate location "/" in fs.inc:1.
How can I solve that?
You need another server block:
server {
listen 443;
server_name bla; # make sure this is the same
# add your ssl specific directives here
location / {
alias /var/www/secured/;
}
}
server {
listen 80;
server_name bla;
return 301 https://$request_uri;
}
This is a global redirect
The idea is to take incoming requests to http://abc.example.com/... and rewrite them to http://example.com/abc/...
That's easy enough to do with a 301/302 redirect:
# rewrite via 301 Moved Permanently
server {
listen 80;
server_name abc.example.com;
rewrite ^ $scheme://example.com/abc$request_uri permanent;
}
The trick is to do this URL change transparently to the client when abc.example.com and example.com point at the same Nginx instance.
Put differently, can Nginx serve the contents from example.com/abc/... when abc.example.com/... is requested and without another client round trip?
Starting Point Config
Nginx config that accomplishes the task with a 301:
# abc.example.com
server {
listen 80;
server_name abc.example.com;
rewrite ^ $scheme://example.com/abc$request_uri permanent;
}
# example.com
server {
listen 80;
server_name example.com;
location / {
# ...
}
}
# abc.example.com
server {
listen 80;
server_name abc.example.com;
location / {
proxy_pass http://127.0.0.1/abc$request_uri;
proxy_set_header Host example.com;
}
}