简体   繁体   中英

Configure nginx with two php-fpm fastcgi_pass locations

For wordpress site using nginx + php-fpm I'm interested in applying a longer fastcgi_read_timeout directive just to /wp-admin directory to avoid timeouts for time intensive admin tasks.

The only issue with the code sample below is when I visit http://webpage.org/wp-admin I get 404. When I visit http://webpage.org/wp-admin/index.php the page posts.

Using nginx add-header directive to help me debug I've been able to determine that when visiting http://webpage.org/wp-admin nginx chooses location ~ .php$ over location ^~ /wp-admin .

Any ideas on how to resolve this? Thanks

location ^/wp-admin/.*.(php|phps)$  {

    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
    fastcgi_keep_conn on;

    fastcgi_read_timeout 120;
}



location ~ \.php$ {

    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
    fastcgi_keep_conn on;

   }
  • I tested change to try_files directive suggested by Keenan Lawrence which resolved issue for me.

  • Helpful explanation of nginx try_files directive can be found here: how can i make this try_files directive work?

  • To troubleshoot this problem I used add_header directive, placing one directive in each location. Then with Chrome browser I opened Developer Tools , clicked on Network tab, clicked on Record Network Log , then loaded the test wp-admin/ page. There is a Header tab that you can then click on to verify where your page loaded. Also see https://serverfault.com/questions/404626/how-to-output-variable-in-nginx-log-for-debugging

  • Working configuration below that includes header directives for debug.

      location ~* ^/wp-admin/.*.(php|phps)$ { add_header X-debug-message "This page processed from location ^~ /wp-admin . uri = $uri ." always; try_files $uri $uri/ =404; fastcgi_split_path_info ^(.+\\.php)(/.+)$; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; fastcgi_keep_conn on; fastcgi_read_timeout 120; } location ~ \\.php$ { add_header X-debug-message "This page processed from location ~ \\.php uri = $uri ." always; try_files $uri =404; fastcgi_split_path_info ^(.+\\.php)(/.+)$; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; fastcgi_keep_conn on; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM