2

I'm using Ubuntu 14.10, nginx 1.6.2, php 5.5.12. I have a site set up like this:

server {
    root /usr/share/nginx/www/oliviataussig;
    index index.php;

    server_name oliviataussig.com www.oliviataussig.com;

    location / {
        try_files $uri $uri/ /index.php =404;
    }

    location ~ \.php$ {
        try_files $uri =404;
        include fastcgi.conf;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
    }
}

When I go to the homepage (index.php), it works fine. However, when I click on any of the links on the page (this is a WordPress-like CMS) I want to fall back to /index.php, and it does that, but index.php gets downloaded instead of executed. How do I fix that?

  • 1
    remove index.php from location / block – Hrvoje Špoljar Nov 20 '14 at 0:46
  • @HrvojeŠpoljar I did that, and then I get a 404 when accessing the menu links (e.g. oliviataussig.com/moving). I want to fall back to /index.php since that script will handle the request and route everything appropriately. I don't want to give a 404. – nnyby Nov 20 '14 at 0:51
  • 1
    that's because you're matching for php$ . If some link is php with arguments and parameters afterwards it will not be matched by php$ – Hrvoje Špoljar Nov 20 '14 at 1:01
  • 1
    Actually /index.php should be in the first try_files but =404 should not. – Michael Hampton Nov 20 '14 at 3:59
  • 1
    codex.wordpress.org/Nginx – Hrvoje Špoljar Nov 20 '14 at 9:20
2

As Hrvoje suggested, Wordpres ->Nginx codex gives examples on most of possible situations (http://codex.wordpress.org/Nginx)

In your case, the config file should look like below:

server {
    root /usr/share/nginx/www/oliviataussig;
    index index.php;

    server_name oliviataussig.com www.oliviataussig.com;

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

    # Add trailing slash to */wp-admin requests.
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;


    # Pass all .php files onto a php-fpm/php-fcgi server.
    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        if (!-f $document_root$fastcgi_script_name) {
            return 404;
        }
        include fastcgi.conf;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
    }
}
  • Thanks a lot - this worked! I omitted the /wp-admin rewrite because this is actually an Indexhibit site, not Wordpress. – nnyby Nov 20 '14 at 21:28
  • How would you get this working in a UserDir? – nnyby Feb 18 '15 at 4:07
0

It's necessary to CLEAR THE CACHE of your browser to see results with the next line.

location / {
    try_files $uri $uri/ /index.php?$args;
}
  • Thanks. This was driving me nuts until I followed your advice and cleared the cache. nginx had been behaving correctly for a while but the browser was stuck with a memory of a bad earlier interaction. – Abid H. Mujtaba Jul 27 '15 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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