Fix: Plesk 11.5 with NGINX and PHP-FPM not rewrite WordPress Page URLs

Posted on June 07, 2013
apache_debian_php_fpm_apc

Plesk 11.5 is still in beta, but shows a lot of potential due to its handling of PHP via PHP-FPM. When active in Plesk 11.5+ PHP-FPM is not properly re-writing WordPress page URLs, and is appending index.html at the end of each URL, causing a 404 error. We have reported the bug to Parallels. In the meantime, the solution can be solved by creating custom nginx.conf directives, either from the command line, or via the Plesk panel.

The Solution code

Adding the code via Plesk Panel

To do this via Plesk Panel, go to the domain’s Web Server settings. At the bottom of the page (where PHP-FPM is activated), there will be a field for ‘Custom NGINX Directives’. Paste the code below into that box.

Plesk 11.5 PHP-FPM settings

Adding the code via Command Line conf file

If you are wanting to do this from the command line, create a ‘vhost_nginx.conf’ file, within the root/conf folder each vhost. For example /var/www/vhosts/domain.com/conf/vhost_nginx.conf’. In that vhost, add the following:

 

Related Posts

  • timber

    I tried this workaround with 11.5.30 #3 but I still get the nginx 404 errors when using permalinks. The WP site works fine with php-fpm and regular links. I noticed that ONLY with php-fpm enabled, wordpress doesnt offer me the pretty permalink option in settings unless I install the WP nginx plugin 2.7.2. I guess it tells WP that mod_rewrite is available. It works fine then with the index.php written into the URLs. What I don’t understand is why pretty permalinks work with nginx without the php-fpm option enabled but not with it being on. Only with php being handled by nginx via php-fpm the rewriting seems to fail resulting in the 404 error. Any ideas anybody?

    • Ruben Ortiz

      HI

      I have noticed that after add code to additional directives Web Stats (domain/plesk-stat) of Plesk fails. It returns a 404.

      Anyone have the same issue?

      • Denis

        having the same issue with stats, still searching for solution

  • Interesting problem. You said that the rewriting seems to fail with the PHP, resulting in a 404 error. What are the error logs reporting when that happens? Can you paste the error? Also, can you paste your NGINX conf file?

    • timber

      Sure. The parts I marked in bold are the lines that get added to the nginx cong when I enable php-fpm in Plesk. Without php-fpm it works fine.

      proxy_access_log:

      AA.BB.CC.DD – – [08/Jul/2013:15:56:17 +0200] “GET /mypage/ HTTP/1.1” 404 162 “-” “Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/536.30.1 (KHTML, like Gecko) Version/6.0.5 Safari/536.30.1”

      last_nginx.conf:

      #ATTENTION!

      #

      #DO NOT MODIFY THIS FILE BECAUSE IT WAS GENERATED AUTOMATICALLY,

      #SO ALL YOUR CHANGES WILL BE LOST THE NEXT TIME THE FILE IS GENERATED.

      server {

      listen AA.BB.CC.DD:80;

      server_name ABCD.com;

      server_name http://www.ABCD.com;

      server_name ipv4.ABCD.com;

      client_max_body_size 128m;

      root “/var/www/vhosts/ABCD.com/httpdocs”;

      access_log /var/www/vhosts/system/ABCD.com/logs/proxy_access_log;

      location / {

      proxy_pass http://AA.BB.CC.DD:7080;

      proxy_set_header Host $host;

      proxy_set_header X-Real-IP $remote_addr;

      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

      access_log off;

      }

      location @fallback {

      proxy_pass http://AA.BB.CC.DD:7080;

      proxy_set_header Host $host;

      proxy_set_header X-Real-IP $remote_addr;

      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

      access_log off;

      }

      location ~ ^/(.*.(ac3|avi|bmp|bz2|css|cue|dat|doc|docx|dts|exe|flv|gif|gz|htm|html|ico|img|iso|jpeg|jpg|js|mkv|mp3|mp4|mpeg|mpg|ogg|pdf|png|ppt|pptx|qt|rar|rm|swf|tar|tgz|txt|wav|xls|xlsx|zip))$ {

      try_files $uri @fallback;

      }

      location ~ ^/~(.+?)(/.*?.php)(/.*)?$ {

      alias /var/www/vhosts/ABCD.com/web_users/$1/$2;

      fastcgi_split_path_info ^((?U).+.php)(/?.+)$;

      fastcgi_param PATH_INFO $fastcgi_path_info;

      fastcgi_pass “unix:/var/www/vhosts/system/ABCD.com/php-fpm.sock”;

      include /etc/nginx/fastcgi.conf; }

      location ~ ^/~(.+?)(/.*)?$ {

      proxy_pass http://AA.BB.CC.DD:7080;

      proxy_set_header Host $host;

      proxy_set_header X-Real-IP $remote_addr;

      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

      access_log off;

      }

      location ~ .php(/.*)?$ {

      fastcgi_split_path_info ^((?U).+.php)(/?.+)$;

      fastcgi_param PATH_INFO $fastcgi_path_info;

      fastcgi_pass “unix:/var/www/vhosts/system/ABCD.com/php-fpm.sock”;

      include /etc/nginx/fastcgi.conf; }

      location ~ /$ {

      index index.html index.cgi index.pl index.php index.xhtml index.htm index.shtml;

      }

      include “/var/www/vhosts/system/ABCD.com/conf/vhost_nginx.conf”;

      }

      fastcgi.conf:

      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

      fastcgi_param QUERY_STRING $query_string;

      fastcgi_param REQUEST_METHOD $request_method;

      fastcgi_param CONTENT_TYPE $content_type;

      fastcgi_param CONTENT_LENGTH $content_length;

      fastcgi_param SCRIPT_NAME $fastcgi_script_name;

      fastcgi_param REQUEST_URI $request_uri;

      fastcgi_param DOCUMENT_URI $document_uri;

      fastcgi_param DOCUMENT_ROOT $document_root;

      fastcgi_param SERVER_PROTOCOL $server_protocol;

      fastcgi_param HTTPS $https if_not_empty;

      fastcgi_param GATEWAY_INTERFACE CGI/1.1;

      fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;

      fastcgi_param REMOTE_ADDR $remote_addr;

      fastcgi_param REMOTE_PORT $remote_port;

      fastcgi_param SERVER_ADDR $server_addr;

      fastcgi_param SERVER_PORT $server_port;

      fastcgi_param SERVER_NAME $server_name;

      # PHP only, required if PHP was built with –enable-force-cgi-redirect

      fastcgi_param REDIRECT_STATUS 200;

      • Your settings look fine. This actually gave me the idea to write a new blog posting some of my PHP-FPM and NGINX CONF settings. I will try to do that in the next day or so. It’s very strange that the server isn’t generating an errors. That’s what I was hoping to see in your server logs, not necessarily the access logs. Try this command to see your errors:

        tail -f /var/www/vhosts/*/logs/error_log /var/log/httpd/error_log /var/log/nginx/error.log

        • timber

          Ok, in addition to the proxy_access_log I get this using your tail command exactly as above:

          ==> /var/log/nginx/error.log <==

          2013/07/08 18:21:26 [error] 3447#0: *8640 "/var/www/vhosts/ABCD.com/httpdocs/mypage/index.html" is not found (2: No such file or directory), client: AA.BB.CC.DD, server: ABCD.com, request: "GET /mypage/ HTTP/1.1", host: "ABCD.com"

          I also tried this with a newly created domain and fresh WP within 11.5.30.

          • timber

            Guess what. I removed this part from the nginx conf:

            location ~ /$ {

            index index.html index.cgi index.pl index.php index.xhtml index.htm index.shtml;

            }

            And now it works. Doh.

    • timber

      I tried to copy&paste the config and error log here but now I can’t see it? When I tried again, it said I’ve already made that comment. Is it waiting for moderation maybe?

      • I just went back and approved it. Please stand by.

  • Hi, Thanks for taking the time to explain this work around.

    Sadly I am still having issues.

    The nginx directives /etc/nginx/plesk.conf.d/vhosts still prevents the Additional
    nginx directives from executing.

    I found that I have to delete this directive from /etc/nginx/plesk.conf.d/vhosts/YOURDOMAIN.conf

    location ~ /$ {
    index index.php index.cgi index.pl index.html index.xhtml index.htm index.shtml;
    }

    Then write the full directive to

    /var/www/vhosts/system/YOURDOMAIN/conf/vhost_nginx.conf

    location ~ /$ {
    index index.php index.cgi index.pl index.html index.xhtml index.htm index.shtml;
    try_files $uri
    $uri/ /index.php?$args;
    }

    My only concern is that as soon as the Nginx conf is
    rewritten, the location ~ /$ directive is reinserted to /etc/nginx/plesk.conf.d/vhosts
    and the fix needs to be re-applied.

    How do we totally remove the directive below from been used
    in /etc/nginx/plesk.conf.d/vhosts?

    location ~ /$ {
    index index.php index.cgi index.pl index.html index.xhtml index.htm
    index.shtml;
    }

    • Hi Mark – I think I understand what you’re trying to do. Instead of manually updated each domain vhost, you should be able to make that change directly from Plesk. I had to do exactly what you’re talking about in an earlier beta, but now any configurations added to the PHP Settings page within Plesk > Domain should stick. Have you tried that yet? Please let me know.

      • Hi Vinny, Yip I did use the Additional nginx directives form in the Web Server Settings tab. I stil have to manually delete the entire location ~ /$ directive from /etc/nginx/plesk.conf.d/vhosts/DOMAIN.conf and restart nginx from the command line 🙁 — Plesk Version: 11.5.30 Update #3, last updated at July 9

  • My site is OFFLINE after using this code plesk 12.0.18

  • 404 error everywhere

  • Kingsley – sorry that you’re having so much trouble. We have paid emergency support services available..please let me know if you are unable to find a solution.

  • Simpel78

    500 Internal Server Error and 404 everywhere..

Contact Us

Have a question? Send us a message. We'll get back to you soon.

captcha

1 Shares
Share
Share1
Tweet
+1
Pin
Stumble