help optimizing nginx to redirect from HTTP/80 to HTTPS/443

I am trying to optimize Nginx (Nginx/0.6.35 + Debian5) to redirect

from HTTP/80 to HTTPS/443

I.e.

any calls made to http://mysite.net/some/uri/ , should go into https://mysite.net/some/uri/

… but calls made to http://mysite.net should go to http://mysite.net

this is the config file from /sites-available/

redirecting www to non-www

server {

listen 80;

server_name www.mysite.net;

rewrite ^/(.*) http://mysite.net/$1 permanent;

}

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.

or

location ~ ^/some/url/.*$ {
  rewrite ^ https://mysite.net$uri permanent;
}

i got this to work pretty good. i just need to make some "improvements/modifications"

this is my current config:

redirecting www to non-www

server {

listen 80;

server_name www.mysite.net;

rewrite ^/(.*) http://mysite.net/$1 permanent;

}

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;
}

````

im getting errors about duplicate location/ when i try this setup.

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

I believe I know what you're looking for. The duplicate location error must be due to some other configuration that I don't know about. You will have at least three server sections:

## 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
  }

  ...
}

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct