简体   繁体   English

Nginx别名和try_files子文件夹不起作用

[英]Nginx alias and try_files to subfolder doesn't work

When I put my php files in /home/tim_test it works fine 当我将我的php文件放在/ home / tim_test中时,它工作正常

When i put my files in /home/tim_test/hmm it does not work. 当我将文件放在/ home / tim_test / hmm中时,它不起作用。 Why not? 为什么不?

server {

    listen 80;
    root /home;
    index index.php;

    location /admin {

        alias /home/admin/;
        try_files $uri $uri.html $uri/ /home/admin/;
        index index.html index.htm index.php;

    }

    location /tim_test/ {

        alias /home/tim_test/;
        try_files $uri $uri.html $uri/ /home/tim_test/;
        index index.html index.htm index.php;

    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

        location ~ \.php$ {

            try_files $uri =404;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;

        }     

}

I tried editing my file to look like this to reflect the folder change, but it doesn't work, I get 403 forbidden when I visit /tim_test in a browser 我尝试编辑文件以使其看起来像这样以反映文件夹更改,但是它不起作用,当我在浏览器中访问/ tim_test时,我被禁止使用403

location /tim_test/ {

    alias /home/tim_test/hmm/;
    try_files $uri $uri.html $uri/ /home/tim_test/hmm/;
    index index.html index.htm index.php;

}

Thank you! 谢谢! I am so stumped. 我好难过 I tried over 100 nginx restarts and different things. 我尝试了100多次nginx重新启动和其他操作。

Edit: If I go there on chrome I get a 404 not found, I think I was only getting the 403 on firefox because I tried visiting the site so many times. 编辑:如果我在chrome上去,则找不到404,我认为我在firefox上只得到403,因为我尝试了很多次。 Either way though, it does not work. 无论哪种方式,它都不起作用。

There is a very old open bug on nginx regarding the alias and try_files directives. 关于aliastry_files指令, nginx上存在一个非常古老的开放错误

Regarding your first configuration file, the two alias directives are unnecessary, as both location blocks inherit the value of root and append the value of the location. 关于第一个配置文件,不需要两个alias指令,因为两个位置块都继承root的值并附加位置的值。

Also, the final element of the try_files directives ( /home/admin/ and /home/tim_test/ ) are not URIs. 另外, try_files指令的最后一个元素( /home/admin//home/tim_test/ )也不是URI。

The first configuration file should be: 第一个配置文件应为:

root /home;
location /admin {
    try_files $uri $uri.html $uri/ /admin/;
    index index.html index.htm index.php;
}
location /tim_test/ {
    try_files $uri $uri.html $uri/ /tim_test/;
    index index.html index.htm index.php;
}

Regarding the subfolder hmm . 关于子文件夹hmm Except for the final element of the try_files directive (which is addressed above) the configuration looks fine. 除了try_files指令的最后一个元素(上面已解决)外,配置看起来还不错。 Which means that the problem may be related to alias / try_files interaction. 这意味着该问题可能与alias / try_files交互有关。

Alternatives to alias are internal rewrites, but placing the target URI within the namespace of the source URI probably makes the solution unnecessarily difficult. alias替代方法是内部重写,但是将目标URI放在源URI的名称空间中可能会使解决方案不必要地困难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM