nginx配置问题(root /别名)

时间:2017-01-14 16:24:38

标签: php nginx configuration

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;
    }

}

include snippets / 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文档页面。

提前致谢。

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

1 个答案:

答案 0 :(得分: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$数据块处理任何不以.php开头的/homelab URI。

location ^~ /homelab块是一个前缀位置,它优先于同一级别的其他正则表达式位置。有关详细信息,请参阅this document

嵌套location ~ \.php$块负责处理位于.php的{​​{1}}下面的/homelab个URI。

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