help optimizing nginx to redirect from HTTP/80 to HTTPS/443
from HTTP/80 to HTTPS/443
I.e.
any calls made to
… but calls made to
this is the config file from /sites-available/
redirecting www to non-www
server {
listen 80;
server_name
rewrite ^/(.*)
}
server {
listen 80;
server_name mysite.net;
access_log /var/log/nginx/mysite.net.access.log;
Default location
location / {
root /srv/vhosts/mysite.net;
index index.html index.htm index.php;
}
.
.
.
.
any help is greatly appreciated.
6 Replies
rewrite ^/some/url/(.*) https://mysite.net/some/url/$1 permanent;
This should work if you put in the 2nd server { } block.
location ~ ^/some/url/.*$ {
rewrite ^ https://mysite.net$uri permanent;
}
this is my current config:
redirecting www to non-www
server {
listen 80;
server_name
rewrite ^/(.*)
}
server {
listen 80;
server_name mysite.net;
access_log /var/log/nginx/mysite.net.access.log;
Default location
location / {
root /srv/vhosts/mysite.net;
index index.html index.htm index.php;
}
Redirect trafic to https
location ~ ^.*$ {
rewrite ^/(.*) https://mysite.net$uri permanent;
}
.
.
.
.
.
i need to have mysite.net/something/ not to be redirected to https.
everything else to be redirected, but not that directory.
is it possible to set it up that way ???
server {
listen 80;
servername mysite.net;
accesslog /var/log/nginx/mysite.net.access.log;
root /srv/vhosts/mysite.net;
index index.html index.htm index.php;
Default location
location / {
rewrite ^ https://mysite.net$uri permanent;
}
location /something {
# any special handling?
# expires 30d;
}
…
````
maybe i didnt explain what im trying to accomplish.
I want mysite.net to be accesible only thru https.
just mysite.net/folder/ to be accesible only thru http
## Send requests with www to non-www domain for both ssl and non-ssl
server {
listen 80;
listen 443;
server_name www.mysite.net;
rewrite ^ $scheme://mysite.net$uri? permanent;
}
## Non-ssl server
server {
listen 80;
server_name mysite.net;
access_log /var/log/nginx/mysite.net.access.log;
root /srv/vhosts/mysite.net;
index index.html index.htm index.php;
## Send all requests to ssl except /folder/
location / {
rewrite ^ https://mysite.net$uri? permanent;
}
## Serve files in this folder directly
location /folder/ {
}
}
## ssl server
server {
listen 443;
server_name mysite.net;
access_log /var/log/nginx/mysite.net.access.log;
root /srv/vhosts/mysite.net;
index index.html index.htm index.php;
ssl on;
ssl_certificate /etc/ssl/certs/mysite.com_combined.crt;
ssl_certificate_key /etc/ssl/private/mysite.com.key;
location / {
# regular handling
}
...
}