Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am replacing lighttpd with nginx on my development server. I got it working with PHP and SSL, but I'm stumped by what should be a simple rewrite. I need to rewrite URLs from




The rule I am using is:

rewrite ^/signup/([0-9]+)$  /signup/index.php?attycode=$1 last;

I have tried numerous variations on this, moved it around, put it inside a location block. What happens is the URL is rewritten to:

The hostname is inserted, and it seems to always lose https and go to http.

My server section is below. I have read and re-read the nginx docs (as they are) and searched the nginx mailing list, but nothing I've tried has solved this problem.

Ubuntu 8.0.4 LTS in case that matters.


server {
    listen      80;
    listen      443 default ssl;
    server_name   localhost;
    root        /var/www/foo;
    index       index.php index.html;
    # ssl cert stuff omitted
    charset utf-8;
    access_log  /var/log/www/dev.access.log  main;

    location ~ /\. {
        deny  all;

    location ~* ^.+\.(inc|tpl|sql|ini|bak|sh|cgi)$ {
        deny all;

    location ~* ^/(scripts|tmp|sql)/ {
        deny all;

    rewrite ^/robots.txt$         /robots_nocrawl.txt break;
    rewrite ^/signup/([0-9]+)$    /signup/index.php?attycode=$1 last;

    location / { 
        try_files $uri $uri/ /error_404.php;

    location ~ \.php$ {
        fastcgi_pass localhost:51115;
        fastcgi_index index.php;
        fastcgi_intercept_errors on; 
        include fastcgi_params;
        fastcgi_param SERVER_NAME       $http_host;
        fastcgi_param SCRIPT_FILENAME   $document_root$fastcgi_script_name;

    error_page  404              /error_404.php;
share|improve this question


rewrite ^/signup/([0-9]+)$  https://$server_namesignup/index.php?attycode=$1 last;

if it comes from HTTP then nginx is gonna try and push it back to HTTP and I think it also rewrites to HTTP by default unless you specify a protocol.

share|improve this answer
I wrote "always lose https and go to http." Even if I put a protocol in the rewrite rule destination. I never did get this to work. Sticking with lighttpd. – gregjor Nov 27 '10 at 8:38
location /signup/ {
     rewrite ^/signup/([0-9]+)$ /signup/index.php?attcode=$1 break;


location ~ ^/signup/([0-9]+)$ {
     rewrite ^ /signup/index.php?attcode=$1 break;

or if you really want to redirect:

location ~ ^/signup/([0-9]+)$ {
     return 301 $scheme://$server_name/signup/index.php?attcode=$1
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.