【问题标题】:nginx configuration issues (root/alias)nginx 配置问题(根/别名)
【发布时间】:2017-05-29 20:47:12
【问题描述】:
server {

    server_name mediaserver.localdomain;
    listen 80;
    index index.php index.htm index.html;
    root /var/www/html/Organizr;
    location = / {
        root /var/www/html/Organizr;
    }
    location /homelab {
        alias /opt/homelab/;
    }
    location ~ /\.git {

        deny all;
    }
    location ~ \.php$ {

    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

}

包括sn-ps/fastcgi-php.com; =

# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;

# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;

# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;

fastcgi_index index.php;
include fastcgi.conf;

这是我的配置,我一辈子都无法让它工作。

我的期望是让 http://mediaserver.localdomain/ 转到“/var/www/html/Organizr/index.php”

当我访问 http://mediaserver.localdomain/homelab/ 时,它会拉取“/opt/homelab/index.php”

但只有 http://mediaserver.localdomain/ 对 /homelab/ 不起作用

我已经用尽了我的谷歌管理技术和关于别名和根定义的 nginx 文档页面。

提前致谢。

仅供参考(我故意在链接中放置空格以摆脱自动链接)

【问题讨论】:

    标签: php nginx configuration


    【解决方案1】:

    您需要从两个根目录执行 PHP 文件,这意味着您的 fastcgi_pass 指令需要两个单独的位置。

    server {
        server_name mediaserver.localdomain;
        listen 80;
        index index.php index.htm index.html;
    
        root /var/www/html/Organizr;
    
        location / {
            try_files $uri $uri/ =404;
        }
        location ~ /\.git {
            deny all;
        }
        location ~ \.php$ {
            try_files $uri =404;
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
    
        location ^~ /homelab {
            root /opt;
            try_files $uri $uri/ =404;
    
            location ~ \.php$ {
                try_files $uri =404;
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            }
        }
    }
    

    location / 块继承了外部块的root,不需要重复。

    第一个location ~ \.php$ 块处理任何不以/homelab 开头的.php URI。

    location ^~ /homelab 块是一个前缀位置,它优先于同一级别的其他正则表达式位置。详情请见this document

    嵌套的location ~ \.php$ 块负责处理/homelab 下位于/opt/homelab 中的.php URI。

    我添加了一些try_files directives,它们也解决了issue of passing uncontrolled requests to PHP

    【讨论】:

    • 谢谢理查德; try_files 是重复的,因为 sn-ps 有: # 在传递它之前检查 PHP 脚本是否存在 try_files $fastcgi_script_name =404;
    • 一旦我删除了 try_files,一切都像宣传的那样工作。感谢您的精彩解释!
    猜你喜欢
    • 1970-01-01
    • 2014-06-03
    • 2018-08-06
    • 1970-01-01
    • 2012-03-17
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多