I have the following http config:
server {
listen 80;
server_name example.com;
# Necessary for Let's Encrypt Domain Name ownership validation
location /.well-known/acme-challenge/ {
root /home/vagrant/.well-known/acme-challenge/;
}
return 301 https://$host$request_uri;
}
I would like http://example.com/.well-known/acme-challenge/filename to serve /home/vagrant/.well-known/acme-challenge/filename while every other http request should be redirected to https.
I thought Nginx would process rules in their order, if matches, uses it, otherwise continues. But apparently not. How can I achieve what I want ?
You should move that redirect to be in the "/" block:
location / {
return 301 https://$host$request_uri;
}
They do process in order, but yours is outside of any location block, so likely is taking precedence.
According to nginx, putting root xxx in a location block is a pitfall.
After some struggle, here's my working config:
server {
listen 80;
server_name example.com;
root /vagrant/www/current/public;
# Necessary for Let's Encrypt Domain Name ownership validation
location /.well-known/acme-challenge/ {
try_files $uri /dev/null =404;
}
location / {
return 301 https://$host$request_uri;
}
}
Related
I have a really weird problem. I own multiple domains let's say example.com, example.pl, example.at, etc. My main domain is example.com, all the rest should be (and mostly are) redirected to example.com for which I own certificate (only for this one). Now what is strange is that if I go to whichever of my domains, doesn't matter if I use www or not, all these domains get properly redirected with code 301 to https://example.com. All except one - www.example.com - this one always gets 307 and ERR_CERT_COMMON_NAME_INVALID. I'm totally lost. Have anyone experienced something like this?
Here is my nginx sites-available config:
server {
listen 80;
server_name example.com;
location / {
return 301 https://example.com$request_uri;
}
}
server {
listen 443 ssl default_server;
server_name example.com;
include /etc/nginx/headers/headers.conf;
include /etc/nginx/ssl-options/options-ssl-nginx.conf;
ssl_dhparam /etc/nginx/ssl-options/ssl-dhparams.pem;
access_log /var/log/nginx/data-access.log combined;
if ($http_host = www.example.com){
return 303 https://example.com$request_uri;
}
if ($http_host != example.com){
return 303 https://example.com$request_uri;
}
location / {
root /usr/share/nginx/html;
try_files $uri /blog-build.html;
}
}
In my /conf.d/ folder I have a .conf file as follows:
server {
listen 80;
listen [::]:80;
return 301 https://www.example.com$request_uri;
}
You can see that it redirect any http traffic to https, that is fine.
However, there are certain http URLs, like http://www.example.com/profile/(.*) that I want to 404 immediately, without the redirect to the https version first.
I have tried variants of the following code with no success, the URL, eg http://www.example.com/profile/abc123, is always redirected to the https version.
server {
listen 80;
listen [::]:80;
location ~* ^/profile/ {
return 404;
}
return 301 https://www.example.com$request_uri;
}
Note that I want to capture example.com/profile/ and any pages that match that, eg example.com/profile/abc123 etc
Thank you.
server {
listen 80;
listen [::]:80;
location ~ ^/profile/ {
return 404;
}
server_name example.com *.example.com;
location ~ ^/ {
return 301 https://www.example.com$request_uri;
}
}
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.
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 been struggling for a while to find a solution for a problem that I have:
I need to redirect all http request to https excepting a specific page under a certain location.
I have the following:
server {
listen 80;
listen 443 ssl;
server_name myserver;
root /var/www/example.org/htdocs;
index index.html;
.
.
}
I can't use:
server {
listen 80;
server_name my.domain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name my.domain.com;
.
.
}
I found a good solution in here
The problem with the solution is that the redirect gets into an infinitive loop, the solution is something like, after my changes:
map $uri $example_org_preferred_proto {
default "https";
~^/post/ "http";
}
server {
listen 80;
listen 443 ssl;
server_name example.org;
root /var/www/example.org/htdocs;
if ($example_org_preferred_proto = "https") {
return 301 $example_org_preferred_proto://example.org$request_uri;
}
}
I understand logic of the problem:
I get a request like: http://wwww.example.com/test -> redirects to https://wwww.example.com/test
Now I get a request like: https://wwww.example.com/test -> redirects to https://wwww.example.com/test
Now I get a request like: https://wwww.example.com/test -> redirects to https://wwww.example.com/test and got into a loop ....
I need a way to stop after redirect one time.
server {
listen 80;
server_name wwww.example.com;
root /var/www/example.org/htdocs;
location /test {
try_files $uri $uri/ =404;
}
location / {
return 301 https://$server_name$request_uri;
}
}
server {
listen 443 ssl;
## here goes other ssl staff which you can find by link:
## https://gist.github.com/paskal/628882bee1948ef126dd
server_name example.org;
root /var/www/example.org/htdocs;
location /test {
try_files $uri $uri/ =404;
}
location / {
return 301 https://$server_name$request_uri;
}
}
Best solution is simplest one. You need to serve only one location and redirect anything else - so do it.
If you have problem with using two server blocks, please describe it in detail.