nginx & wordpress – config for permalinks doesn’t work; downloads php-files instead

I’ve installed nginx and I try to run wordpress on it.
Everything works fine, except for permalinks.

Here is the vhost-file I’m using:

Read More
server {
listen 123456:80;
server_name my-domain.com;

if ($host ~* www.(.*)) {
    set $wwwless $1;
    rewrite ^(.*)$ $scheme://$wwwless$1 permanent;
}

root /var/www/my-folder;

index index.php;

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

    location ~ .php$ {
        fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

(I’ve replaced critical data in the above code with my-domain.com, my-folder and the ip 123456.)

index.php, the admin-panel and using the standard links (…/?p=123) work finde. If I enable some of the permalinks, index.php and the admin-panel still work. But if I try to open another site of the wordpress blog, my browser downloads the index.php 🙁

Related posts

Leave a Reply

3 comments

  1. Try something like this for the .php location config:

    location ~ .php$ {
        include     /etc/nginx/fastcgi_params;
        fastcgi_pass    127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    
  2. Here’s my wordpress config modified to suit you.

    server { # the redirecting from www to non-www
      server_name www.example.com;
      return 301 $scheme://example.com$request_uri$is_args$query_string;
    }
    
    server {
      listen  80;
      server_name example.com;
      root /path/to/root;
      # make sure the directory /var/log/nginx exists
      access_log /var/log/nginx/wordpress_access.log;
      error_log /var/log/nginx/wordpress_error.log;
      index index.php;
      location / {
        try_files $uri /index.php$request_uri$is_args$query_string;
      }
      location ~ .php {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
      }
      location ~ /.ht { # avoid downloading htaccess, htpasswd, etc files
        deny all;
      }
    }
    
  3. you can try this vhost file

    server {
       listen 80;
       server_name www.example.com example.com;
       root /var/www/www.example.com/web;
       if ($http_host != "www.example.com") {
                 rewrite ^ http://www.example.com$request_uri permanent;
       }
       index index.php index.html;
       location = /favicon.ico {
                log_not_found off;
                access_log off;
       }
       location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
       }
       # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
       location ~ /. {
                deny all;
                access_log off;
                log_not_found off;
       }
       location / {
                try_files $uri $uri/ /index.php?$args;
       }
       # Add trailing slash to */wp-admin requests.
       rewrite /wp-admin$ $scheme://$host$uri/ permanent;
       location ~*  .(jpg|jpeg|png|gif|css|js|ico)$ {
                expires max;
                log_not_found off;
       }
       location ~ .php$ {
                try_files $uri =404;
                include /etc/nginx/fastcgi_params;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       }
    

    }