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 / 问题 / 716215
Accepted
Naftuli Kay
Naftuli Kay
Asked: 2015-08-24 21:58:39 +0800 CST2015-08-24 21:58:39 +0800 CST 2015-08-24 21:58:39 +0800 CST

多个静态文件目录,单个 PHP FPM 服务器

  • 772

我有两个目录需要提供静态资产:

  1. /srv/web: 静态资源,包括图片、JavaScript、HTML 等。
  2. /srv/php:动态 PHP 脚本以及一些静态资产。

我正在使用 NGINX 并像这样配置它:

server {
    listen 80;
    server_name _;

    # root /;
    index index.php index.html index.htm;
    try_files /srv/web/$uri /srv/php/$uri =404;

    location ~ \.php$ {
        root /srv/php;
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
#       fastcgi_param SCRIPT_FILENAME /srv/php$fastcgi_script_name;
        include fastcgi_params;
    }
}

我在 Ubuntu 14.04,PHP FPM 包版本是 5.5.9,NGINX 包版本是 1.4.6。

这里的简单目标是首先提供静态文件/srv/web,失败/srv/php,失败,返回 404。所有以 结尾的文件\.php$都将通过 Unix 套接字从 PHP FPM 请求,这是有效的。

我目前遇到的问题是关于 的index指令server没有按计划工作。我有一个index.html文件/srv/web,当我这样做时

curl -is http://localhost/

我得到一个404。

这是设置具有多个文件系统文件夹以与 PHP 一起提供服务的 NGINX 站点的最理想方式吗?关于为什么我的静态索引不起作用的任何想法?


更新

根据下面 AD7six 的回答,我已将配置更新为如下所示:

server {
    listen 80;
    server_name _; # listen at all host names

    # serve static files first from /srv/web, then from /srv/php, and any dynamic PHP files from
    # FastCGI/FPM at the Unix socket.
    location / {
        root /srv/web;
        index index.html index.htm;
        try_files $uri $uri/ @php;
    }

    location @php {
        root /srv/php;
        index index.php;
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        root /srv/php;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /srv/php/$fastcgi_script_name;
        include fastcgi_params;
    }
}

我的目录列表如下所示:

/srv/
|-- php
|   |-- demo.php
|   |-- index.php
|   `-- phpstatic.txt
`-- web
    |-- static.html
    `-- subdir
        `-- index.html

3 directories, 5 files

获取静态文件和 PHP 文件按计划工作,获取/subdir/其索引工作正常,但如果我 GET /,我得到一个 403 禁止,并且 nginx 抱怨目录列表:

2015/08/24 21:50:59 [error] 9159#0: *7 directory index of "/srv/web/" is forbidden, client: 127.0.0.1, server: _, request: "GET / HTTP/1.1", host: "localhost"

不知道为什么会失败,但至少闻起来像是进步。

nginx
  • 1 1 个回答
  • 2683 Views

1 个回答

  • Voted
  1. Best Answer
    AD7six
    2015-08-25T00:31:21+08:002015-08-25T00:31:21+08:00

    多个根不会那样工作

    使用此配置:

    server {
        # root /;
        index index.php index.html index.htm;
        try_files /srv/web/$uri /srv/php/$uri =404;
    

    没有使用 index 指令的请求处理,因为请求必须匹配文件。使用调试日志证实了这一点:

    2015/08/24 08:12:11 [debug] 17173#0: *26 try files phase: 13
    2015/08/24 08:12:11 [debug] 17173#0: *26 http script copy: "/srv/web/"
    2015/08/24 08:12:11 [debug] 17173#0: *26 http script var: "/"
    2015/08/24 08:12:11 [debug] 17173#0: *26 trying to use file: "/srv/web//" "/srv/web//"
    2015/08/24 08:12:11 [debug] 17173#0: *26 http script copy: "/srv/php/"
    2015/08/24 08:12:11 [debug] 17173#0: *26 http script var: "/"
    2015/08/24 08:12:11 [debug] 17173#0: *26 trying to use file: "/srv/php//" "/srv/php//"
    2015/08/24 08:12:11 [debug] 17173#0: *26 trying to use file: "=404" "=404"
    

    使用try_files确实会查找如下目录的指令:

    try_files /srv/web/$uri /srv/web/uri/ /srv/php/$uri /srv/php/$uri/ =404;
    

    也不起作用:

    2015/08/24 08:16:17 [debug] 17651#0: *33 http script copy: "/srv/web/"
    2015/08/24 08:16:17 [debug] 17651#0: *33 http script var: "/srv/web//index.html"
    2015/08/24 08:16:17 [debug] 17651#0: *33 trying to use file: "/srv/web//srv/web//index.html" "/srv/web//srv/web//index.html"
    2015/08/24 08:16:17 [debug] 17651#0: *33 http script copy: "/srv/web/"
    2015/08/24 08:16:17 [debug] 17651#0: *33 http script var: "/srv/web//index.html"
    2015/08/24 08:16:17 [debug] 17651#0: *33 trying to use dir: "/srv/web//srv/web//index.html" "/srv/web//srv/web//index.html"
    2015/08/24 08:16:17 [debug] 17651#0: *33 http script copy: "/srv/php/"
    2015/08/24 08:16:17 [debug] 17651#0: *33 http script var: "/srv/web//index.html"
    2015/08/24 08:16:17 [debug] 17651#0: *33 trying to use file: "/srv/php//srv/web//index.html" "/srv/php//srv/web//index.html"
    2015/08/24 08:16:17 [debug] 17651#0: *33 http script copy: "/srv/php/"
    2015/08/24 08:16:17 [debug] 17651#0: *33 http script var: "/srv/web//index.html"
    2015/08/24 08:16:17 [debug] 17651#0: *33 trying to use dir: "/srv/php//srv/web//index.html" "/srv/php//srv/web//index.html"
    2015/08/24 08:16:17 [debug] 17651#0: *33 trying to use file: "=404" "=404"
    

    请注意,“root”是混淆的,try_files需要一个 url 而不是文件路径。我建议不要继续尝试使用这种解决方案 - 特别是不要将 root 设置为/并可能允许访问服务器上的任何文件。

    使用两个位置块

    相反,让事情变得简单。此配置将提供所有静态内容:

        root /srv/web;
        index index.html;
        try_files $uri $uri/;
    

    此配置服务于所有 php 内容:

        root /srv/php;
        index index.php;
        try_files $uri $uri/;
    

    把它们放在一起:

    location / {
        root /srv/web;
        index index.html;
        try_files $uri $uri/ @php;
        error_page 403 = @php; # see note below
    }
    
    location @php {
        root /srv/php;
        index index.php;
        try_files $uri $uri/ =404;
    }
    
    location ~ \.php$ {
        # as before
    }
    

    一个问题是,通过这种设置,与没有文件的文件夹匹配的请求/srv/web将引发403index.html错误(因为请求是针对目录的,并且没有目录索引文件)。为了允许这些请求也由 php 处理 - 有必要将 403 错误重定向到 php 处理程序。

    • 2

相关问题

  • 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