Linode Forum
Linode Community Forums
 FAQFAQ    SearchSearch    MembersMembers      Register Register 
 LoginLogin [ Anonymous ] 
Post new topic  Reply to topic
Author Message
PostPosted: Thu Jul 08, 2010 11:22 pm 
Offline
Senior Newbie

Joined: Sun Apr 11, 2010 5:45 pm
Posts: 10
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.


Top
   
 Post subject:
PostPosted: Fri Jul 09, 2010 6:20 am 
Offline
Senior Newbie

Joined: Thu Dec 24, 2009 5:36 pm
Posts: 12
Code:
rewrite ^/some/url/(.*) https://mysite.net/some/url/$1 permanent;

This should work if you put in the 2nd server { } block.


Top
   
 Post subject:
PostPosted: Sat Jul 10, 2010 10:10 pm 
Offline
Senior Member

Joined: Sun Feb 21, 2010 5:12 pm
Posts: 64
or
Code:
location ~ ^/some/url/.*$ {
  rewrite ^ https://mysite.net$uri permanent;
}


Top
   
 Post subject:
PostPosted: Tue Sep 14, 2010 9:07 pm 
Offline
Senior Newbie

Joined: Sun Apr 11, 2010 5:45 pm
Posts: 10
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 ???


Top
   
 Post subject:
PostPosted: Tue Sep 14, 2010 9:17 pm 
Offline
Senior Member

Joined: Sun Feb 21, 2010 5:12 pm
Posts: 64
Code:
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;

## Default location
  location / {
    rewrite ^ https://mysite.net$uri permanent;
  }

  location /something {
    # any special handling?
    # expires 30d;
  }

...


Top
   
 Post subject:
PostPosted: Wed Sep 15, 2010 12:30 pm 
Offline
Senior Newbie

Joined: Sun Apr 11, 2010 5:45 pm
Posts: 10
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


Top
   
 Post subject:
PostPosted: Wed Sep 15, 2010 12:54 pm 
Offline
Senior Member

Joined: Sun Feb 21, 2010 5:12 pm
Posts: 64
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:

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

  ...
}


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
RSS

Powered by phpBB® Forum Software © phpBB Group