2

I'm trying to set up my nginx-server to accomplish the following

x.com opens index.php in /

x.com/example opens example.php in /

x.com/example/ opens example.php in /

x.com/foo/example opens example.php in /foo/

I have the following configuration

server {
listen www.delibr.com:443 default_server;  # if this is not a default server, remove "default_server"
root /var/delibr.se;
index index.php index.html index.htm; # this is also irrelevant

server_name www.delibr.com;
ssl_certificate /path to/crt;
ssl_certificate_key /path to/key;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers AES256+EECDH:AES256+EDH:!aNULL;

location /beta {
    return 302 /#signup-form;
}

location / {
   try_files $uri $uri.php $uri/;
    if ($host != 'www.delibr.com') {
        rewrite     ^ https://www.delibr.com$request_uri? permanent;
    }
}

location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

    # With php5-fpm:
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    }

}

With this setup /example downloads example.php and /example/ gives 500 Internal Server error. Can anyone help me?

1 Answer 1

2

You try_files should be as below

try_files $uri $uri/ $uri.php;

All parameter except the last one is tested in current context. So you when you use $uri.php and the file is found, the current context is meant to serve it as a static file, which it does. But for the last parameter it will check against other locations blocks in the config.

Edit-1

Your request for x.com/example/ opens example.php in / is not a usual one and it was a bit tricky to make it work. When we have the $uri.php in try_files it changes the url to x.com/example/.php. So we need to handle this url additionally by adding one additional block

location ~* /\.php {
   rewrite ^(.*)/\.php$ $1.php redirect;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! Almost worked. But it didn't solve x.com/example/ opens example.php in / it generates "No input file specified."
Yes that is obvious let me check something quick
I tried to add rewrite ^/(.*)/$ /$1 permanent;and that solves it but generates a new problem. Then subfolders that actually have a index.html doesn't work.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.