I have http:// and https:// on the same host like the following:
server {
listen 80;
listen 443 ssl;
...
...
}
What I need to do is redirecting users who access my shop to https://. The problem is I have many languages:
https://mydomain.com/en/shop https://mydomain.com/fr/shop etc...
I tried this and it didn't work (nginx: configuration file /etc/nginx/nginx.conf test failed)
:
if ($server_port = 80) {
location (en|fr)/shop {
rewrite ^ https://$host$request_uri permanent;
}
}
Or better yet, avoiding the hardcoded server name
Ideally, avoiding if statements while preserving the trailing path:
permanent takes care of the 301.
In order to use regular expressions for matching
location
s, you need to prefix the expression with either~
or~*
:From the documentation:
Since nginx does't allow
location
blocks to be nested inside ofif
blocks, try the following configuration:another way with error_page 497
It would also be more of an NGINX best practice to do a 301 redirect instead of using the if statement (see Server name on http://wiki.nginx.org/Pitfalls). I created a gist with an nginx.conf configured for SSL, Rails and Unicorn
https://gist.github.com/Austio/6399964
Here would be the relevant section for yours.