2

I want to redirect http to https along with

rewrite ^(.*)$ /$1.php;

My nginx virtualhost file:

server {
   listen         80;
   server_name    domain.com;
   return         301 https://$server_name$request_uri;
}

server {
   listen         443 ssl;
   server_name    domain.com;
   root /var/nginx/html;

   ssl_certificate /home/domain.crt
   ssl_certificate_key /home/domain.key

   location / {
            if (!-e $request_filename){
                    rewrite ^(.*)$ /$1.php;
            }
            try_files $uri $uri/ /index.html;
   }

    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
          root /usr/share/nginx/www;
    }

    # pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm$
    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_nam$
            include fastcgi_params;

    }

}

…but this gives me a redirect loop. Can somebody point me what am I doing wrong?

0

1 Answer 1

1
if (!-e $request_filename){
    rewrite ^(.*)$ /$1.php;
}
try_files $uri $uri/ /index.html;

What happens if /$1.php does not exist either? I'm no expert on Nginx unfortunately, but on Apache this sort of rewrite could result in a rewrite loop (you would need to check that the file exists first before rewriting to it).

Should this not be rewritten using try_files only...? For example:

try_files $uri $url.php $uri/ /index.html;
2
  • No, nginx says Not found when such file is not present on server.
    – Deep S.
    Aug 31, 2015 at 17:30
  • try_files $uri $url.php $uri/ /index.html; Thanks this did the job!
    – Deep S.
    Sep 1, 2015 at 7:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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