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 / 问题 / 878072
Accepted
jma
jma
Asked: 2017-10-12 22:36:58 +0800 CST2017-10-12 22:36:58 +0800 CST 2017-10-12 22:36:58 +0800 CST

没有初始路径组件的 nginx 位置

  • 772

我有一个看起来像这样的位置块:

root /var/www/ProjectP;

location /p/ {
    index index.php;
    try_files $uri $uri/ =404;
}

location ~ /p/.*\.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

为了完成这项工作,我创建了一个目录/var/www/ProjectP/p/,这是一个可怕的 hack,因为它提供了项目在 http 空间中的位置的知识,而不是 nginx。Nginx 配置应该能够在/pp/不修改项目 P的情况下将其移动到。

这个特定的项目没有proxy_pass指令,但接下来会发生,然后我的 hack 不太可能奏效。

我确信 nginx 有一个正确的方法来处理如此常见的事情,但我还没有找到它。任何指针?

更新 1

正如@RichardSmith 所说,正确的方法肯定是别名指令。在静态内容上测试这个效果很好:

location /a/ {
    alias /var/www/ProjectA/;
    index index.html;
    try_files $uri $uri/ =404;
}

当我对 php 代码做同样的事情时,我得到了一半的成功:

location /p/ {
    alias /var/www/ProjectP/;
    index index.php;

    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ /p/(.+\.php)$ {
    alias /var/www/ProjectP/$1;
    include snippets/fastcgi-php.conf;

    # With php7.0-fpm:
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    ## These next are largely set via snippets/fastcgi-php.conf.
    ## Trying them here doesn't help, but I leave them (commented out)
    ## for the moment...
    #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    #include fastcgi_params;
    #fastcgi_param SCRIPT_FILENAME $uri;
}

针对非 php 文件进行测试,我确认第一个块有效。但是第二个块没有,所以 php 文件不能正常服务。(我得到 404。)我在最后注释掉的 fastcgi 指令上尝试了一些变体,但显然不是正确的组合。

更新 2

这行得通。

root /var/www/;

location /p/ {
    alias /var/www/ProjectP/;
    index index.php;

    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;

    # Pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;

        # With php7.0-fpm:
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $request_filename;
    }
}
nginx
  • 2 2 个回答
  • 6943 Views

2 个回答

  • Voted
  1. Best Answer
    jma
    2017-10-24T05:29:58+08:002017-10-24T05:29:58+08:00

    这行得通。

    root /var/www/;
    
    location /p/ {
        alias /var/www/ProjectP/;
        index index.php;
    
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    
        # Pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
    
            # With php7.0-fpm:
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $request_filename;
        }
    }
    
    • 1
  2. Tero Kilkanen
    2017-10-13T10:53:59+08:002017-10-13T10:53:59+08:00

    location您的区块至少存在一个问题。

    如果您要求http://www.example.com/p/test.php,$1变量的内容将为test.php.

    这意味着您的

    alias /var/www/ProjectP/$1;
    

    将使文档根目录变为/var/www/ProjectP/test.php.

    然后,

    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    

    定义发送到 PHP 进程以定位文件的实际文件路径。$document_root包含此alias转换中的路径,并$fastcgi_script_name包含来自请求的文件名。

    因此,在您的情况下,最终结果是请求http://www.example.com/p/test.php最终成为/var/www/ProjectP/test.phptest.phpPHP 进程的文件名。因此会有404退货。

    解决方法是只使用:

    alias /var/www/ProjectP
    

    在 PHPlocation块中。

    • 0

相关问题

  • 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