AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 884981
Accepted
mahatmanich
mahatmanich
Asked: 2017-11-25 01:59:34 +0800 CST2017-11-25 01:59:34 +0800 CST 2017-11-25 01:59:34 +0800 CST

nginx 多个(现在是两个)php 项目通过 php-fpm 在子目录中使用别名

  • 772

对于 nginx 的热爱,我无法解决这个问题。

期望:我想要两个简单的 php 项目(从长远来看是 wordpress)在一个服务器块下的两个子位置。旁注:这些项目位于使用 capistrano 部署的服务器上的两个不同目录中。

问题:我最终得到 404、403 或 index.php 的直接八位字节流下载。在后者上,我似乎找到了正确的 index.php,但它没有传递给 php-fpm 块。php-fpm 工作正常而不是问题(在其他没有子位置的服务器块中测试)

我已经浏览了整个网络,并尝试了数以百万计的“工作”配置,但它并没有融合在一起。

计划:下面你会看到一个工作的 nginx 虚拟主机,在正确的别名目录中点击正确的 index.html 文件。这样我就成功了一半。

在您的帮助下,我想调整下面的配置,将 index.php 更改index为 index.php 并让 php 处理location /staging和/production.

在location /production你看到一个配置(注释掉)我是如何尝试让 php 工作的。

server {
  listen 82;
  listen [::]:82;

  server_name nginx-web.ch;

  access_log /var/log/nginx/nginx-web_access.log;
  error_log /var/log/nginx/nginx-web_error.log;

  location  /staging {
    alias /var/www/nginx-web1/current;
    index index.html
    add_header X-debug-message "Location web1";
  }

  location /production {
    alias /var/www/nginx-web/current;
    index index.html
    add_header X-debug-message "Location web";

    #try_files $uri $uri/ /production/index.php;

    #location ~ \.php$ {
      # add_header X-debug-message "Location ~ php";
      # try_files $uri =404;
      # fastcgi_split_path_info ^(.+\.php)(/.+)$;
      # fastcgi_pass unix:/var/run/php5-fpm.sock;
      # fastcgi_index index.php;
      # include fastcgi_params;
      # fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    #}
  }
}

这是一个工作服务器块,它试图适应子位置,但没有成功:(

server {
  listen 80;
  listen [::]:80;

  server_name testdev;

  access_log /var/log/nginx/wp_access.log;
  error_log  /var/log/nginx/wp_error.log;

  root /var/www;
  index index.php;

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

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

使用 WORKING CONFIG 更新(必须 <3 serverfault/stackoverflow):

这是最终的工作配置,非常感谢@RichardSmith

server {
    listen 82;
    listen [::]:82;

    server_name nginx-web.ch;

    access_log /var/log/nginx/nginx-web_access.log;
    error_log /var/log/nginx/nginx-web_error.log;

    index index.php;

    location ^~ /staging/ {
      alias /var/www/nginx-web1/current/;

      if (!-e $request_filename) { rewrite ^ /staging/index.php last; }

      location ~ \.php$ {
       if (!-f $request_filename) { return 404; }

       include fastcgi_params;
          fastcgi_param  SCRIPT_FILENAME $request_filename;
          fastcgi_pass unix:/var/run/php5-fpm.sock;
       }
    }

    location /production {
      alias /var/www/nginx-web/current;

      if (!-e $request_filename) { rewrite ^ /production/index.php last; }

      location ~ \.php$ {
        if (!-f $request_filename) { return 404; }

        include fastcgi_params;
          fastcgi_param  SCRIPT_FILENAME $request_filename;
          fastcgi_pass unix:/var/run/php5-fpm.sock;
        }
    }
}
nginx
  • 1 1 个回答
  • 1994 Views

1 个回答

  • Voted
  1. Best Answer
    Richard Smith
    2017-11-25T02:48:59+08:002017-11-25T02:48:59+08:00

    这种模式有效:

    location ^~ /prefix/ {
        alias /path/to/root/;
        if (!-e $request_filename) { rewrite ^ /prefix/index.php last; }
    
        location ~ \.php$ {
            if (!-f $request_filename) { return 404; }
    
            include        fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME $request_filename;
            fastcgi_pass   ...;
        }
    }
    

    使用^~前缀以避免其他正则表达式location块优先。请参阅此文档。

    location和两者的值alias都以 结尾/或都不以 结尾/。请参阅此文档。

    由于此问题,请避免一起使用and并查看alias有关使用.try_filesif

    用作(因为它同时适用于和)$request_filename的计算值。SCRIPT_FILENAMEaliasroot

    始终fastcgi_param 在包含fastcgi_params文件后设置,以避免后者默默地覆盖本地值。

    • 1

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve