NGINX - make location on server block to use another server block - nginx

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.

Related

How to redirect traffic nginx in dynamic mode

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 subdomain to a folder?

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.

Rewriting wildcard domain with HTTPS

Is it possible to rewrite HTTPS wildcard Domains in nginx or should we create multiple structure /file for each domain?
Lets say i have follwing:
1. subdomain1.domain.com
2. subdomain2.domain.com
If i do not have HTTPS i used the following which works great:
server {
listen 443;
server_name *.domain.com;
charset utf-8;
}
No if i use HTTPS, i would have to write a new block like the following ( using letsencryt)
The following is just a test domain (only 1 domain )
server {
server_name test.me;
rewrite ^ https://test.me$request_uri? permanent;
}
server {
listen 443;
server_name test.me;
charset utf-8;
...
}
Is it possible to do the same for multiple domains?
server {
server_name .domain.com;
rewrite ^ https://.domain.com$request_uri? permanent;
}
server {
listen 443;
server_name *.domain.com;
charset utf-8;
...
}
I tried the above config but it doesnot work, it redirects me to
https://%2A.domain.com.domain.com/ (just for test)
Is it possible to do something like this ? or Should i have different block for every subdomain ?
Use one of the variables provided by nginx to extract the host name from the request line. For example $host (see this document for details):
server {
server_name .domain.com;
return 301 https://$host$request_uri;
}

nginx duplicate location "/" error

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

Nginx convert subdomain to path component without 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;
}
}

Resources