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;
}
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 would like to redirect multiple subdomains on my server to another with Nginx. Here is what I am doing so far:
server {
listen 80;
server_name firstsub.example.com;
return 301 $scheme://firstsub.anothersite.co$request_uri;
}
server {
listen 80;
server_name secondsub.example.com;
return 301 $scheme://secondsub.anothersite.co$request_uri;
}
because I have about 10 subdomains, adding it like that would be really ugly. Is there a way to write several domain redirects in one server block? How?
If all of the domains have a consistent pattern, you can use a regular expression with the server_name directive.
For example:
server {
listen 80;
server_name ~^(www\.)?(?<domain>.+)\.example\.com$;
return 301 $scheme://$domain.anothersite.co$request_uri;
}
See this document for details.
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.
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.