0

In my nginx config, I intended to reach purposes below:

  1. forward www to non-www
  2. forward https to http
  3. only use https on uri with pattern 'secure'

Code below reach the goals, except that at point 3, Chrome shows blank page with error 'This webpage has a redirect loop'. Anybody can tell proper config for this, and/or any better code?

   #redirect www to non-www
   server {
       listen 80;
       server_name www.example.org;
       return 301 http://example.org$request_uri;
   }

   #redirect https to http, for both www and non-www
   #but process https for uri with pattern 'secure'
   server {
       ssl_certificate /home/mydomain/ssl.cert;
       ssl_certificate_key /home/mydomain/ssl.key;

       listen 443 ssl;
       server_name www.example.org example.org;
       root /home/mydomain/public_html/mydomain/public;
       index index.php;

       access_log /var/log/virtualmin/example.org_access_log;
       error_log /var/log/virtualmin/example.org_error_log;

       #if has pattern 'secure', just process
       #---problem is here, chrome result 'This webpage has a redirect loop'---
       location ~ ^/(secure) {

            try_files $uri $uri/ /index.php$is_args$args;

            location ~ \.php$ {
               try_files $uri =404;
               fastcgi_pass   127.0.0.1:9000;
               fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
               include        fastcgi_params;
           }
       }

       return 301 http://example.org$request_uri;
   }

   server {
       listen 80;
       server_name example.org;
       root /home/mydomain/public_html/mydomain/public;
       index index.php;

       access_log /var/log/virtualmin/example.org_access_log;
       error_log /var/log/virtualmin/example.org_error_log;

       #rewrite to https if has pattern secure
       location ~ ^/(secure) {
           #rewrite ^(.*) https://$host$1 permanent;
           return 301 https://example.org$request_uri;
       }

       location / {
           try_files $uri $uri/ /index.php$is_args$args;
       }

       location ~ \.php$ {
           try_files $uri =404;
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
           include        fastcgi_params;
       }
    }
4
  • What is the actual redirect loop that is generated by this configuration? Show some example URLs. Apr 12, 2015 at 10:10
  • Example URL for the problem: example.org/secure/sessions/login, since it has 'secure' in uri, forwarded to https ://example.org/secure/sessions/login Apr 12, 2015 at 11:33
  • So the loop is between http and https? Apr 12, 2015 at 13:59
  • Likely, yes.... Apr 13, 2015 at 3:15

2 Answers 2

1

Ended by doing ssl redirection in application level

Route::filter('check.ssl', function()
{
    $segment = Request::segment(1);

    if ( ( $segment == 'secure') && ( !Request::secure() ) )
        return Redirect::secure(Request::getRequestUri());
    else if ( ( $segment != 'secure') && ( Request::secure() ) )
        return Redirect::to(Request::path(), 302, array(), false);

});
0

Replace this:

       location ~ ^/(secure) {

        try_files $uri $uri/ /index.php$is_args$args;

        location ~ \.php$ {
           try_files $uri =404;
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
           include        fastcgi_params;
       }
   }

   return 301 http://example.org$request_uri;

With this:

   location / {
        try_files $uri $uri/ /index.php$is_args$args;
   }

   if ( $uri !~ ^/(secure) ){
        return 301 http://riwa.nu$request_uri;
   }

   location ~ \.php$ {
           try_files $uri =404;
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
           include        fastcgi_params;
   }

And it should work fine :-)

1
  • thank you for your answer. Your logic is ok, I think it should work too, but it didn't. Just same error. I also try to move location / { ... under the if , still no lucky. Apr 11, 2015 at 22:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .