【问题标题】:Configure nginx with two php-fpm fastcgi_pass locations使用两个 php-fpm fastcgi_pass 位置配置 nginx
【发布时间】:2017-03-29 19:26:05
【问题描述】:

对于使用 nginx + php-fpm 的 wordpress 站点,我有兴趣将更长的 fastcgi_read_timeout 指令应用于 /wp-admin 目录以避免时间密集型管理员超时任务。

下面代码示例的唯一问题是当我访问 http://webpage.org/wp-admin 时,我得到 404。当我访问 http://webpage.org/wp-admin/index.php 时,页面帖子。

使用 nginx add-header 指令来帮助我调试我已经能够确定当访问http://webpage.org/wp-admin nginx 时选择 location ~ .php$ 而不是 位置^~ /wp-admin。

关于如何解决这个问题的任何想法?谢谢

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;

   }

【问题讨论】:

  • 愚蠢的建议。在 /wp-admin 块的 try_files 指令中,您可能还想在返回 404 之前尝试将 URI 作为目录(因为 wp-admin 是 Wordpress 中的目录)。所以修改它以读取 try_files $uri $uri/ =404;优先,你的配置是正确的。
  • 谢谢,您的建议奏效了。要了解为什么我在这里找到了一个有用的链接stackoverflow.com/questions/17798457/…。
  • 是的,这就是目的。很高兴你让它工作:) P.S 你也可以使用curl -I http://webpage.org/wp-admin 从命令行界面(Unix)检查标题,但 Chrome 调试或 Firebug 也同样好。

标签: php nginx fastcgi nginx-location


【解决方案1】:
  • 我测试了 Keenan Lawrence 建议的对 try_files 指令的更改,它为我解决了问题。

  • nginx try_files指令的有用解释可以在这里找到:how can i make this try_files directive work?

  • 为了解决这个问题,我使用了 add_header 指令,在每个位置放置一个指令。然后用 Chrome 浏览器打开 Developer Tools,点击 Network 标签,点击 Record Network Log,然后加载测试 wp-admin/ 页面.有一个 Header 选项卡,然后您可以单击该选项卡来验证您的页面加载位置。另见https://serverfault.com/questions/404626/how-to-output-variable-in-nginx-log-for-debugging

  • 下面的工作配置包括用于调试的标头指令。

     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;
    
       }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    • 2012-12-23
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    • 2017-05-11
    相关资源
    最近更新 更多