使用PHP的Nginx子目录根目录 [英] Nginx subdirectory root with PHP

查看:129
本文介绍了使用PHP的Nginx子目录根目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在docker容器中运行nginx.我想要一个子目录/web/来访问我的个人文件和项目.它也应该支持php.

I'm running nginx in a docker container. I want to have a subdirectory /web/ to access my personal files and projects. It should also support php.

以下是我正在运行的内容,但domain-a.com/web始终显示404.由于相同的php块可在子域上直接运行,而直接在server{}块中,因此PHP被确认可以正常工作.

Below is what I'm running with but domain-a.com/web keeps resulting in a 404. PHP is confirmed working since the same php block works on a subdomain but directly in a server{} block.

http {

    server {
        listen      443 ssl;
        server_name domain-a.com domain-b.com;

        # Mime types
        include /etc/nginx/confs/mime.types;

        # SSL
        include /etc/nginx/confs/nginx-ssl.conf;

        # Proxy to organizr
        # This works
        location / {
            proxy_pass http://organizr/;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;

            # HTTP 1.1 support
            proxy_http_version 1.1;
            proxy_set_header Connection "";
        }

        # Root folder for my personal files/projects
        # Doesn't work
        location /web {
            index index.php index.html;
            root /etc/nginx/www;

            location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass php:9000;
                fastcgi_index index.php;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_path_info;
            }
        }
    }
}

推荐答案

如果文件位于/etc/nginx/www中,则需要使用alias指令,而不是root指令.有关详细信息,请参见本文档.

If your files are in /etc/nginx/www you will need to use an alias directive, rather than a root directive. See this document for details.

例如:

location ^~ /web {
    index index.php index.html;
    alias /etc/nginx/www;

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

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

        fastcgi_pass php:9000;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $request_filename;
    }
}

使用$request_filename获取别名文件的正确路径.由于此问题,请避免将try_filesalias一起使用.有关使用if的信息,请参见此警告.

Use $request_filename to obtain the correct path to the aliased file. Avoid try_files with alias due to this issue. See this caution on the use of if.

这篇关于使用PHP的Nginx子目录根目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆