nginx alias+location directive

 ̄綄美尐妖づ 提交于 2019-12-03 17:19:48

问题


server {
    listen      80;
    server_name  pwta; 
    root html;

    location /test/{
        alias html/test/;
        autoindex on;
    }
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

This configuration works. However, if location /test/ is replaced e.g. location /testpath/ it doesn't work (No input file specified). I assumed base on the explanation of alias directive that the "location" part is dropped and thus /testpath/info.php would result in html/test/info.php.

Thanks for any suggestion.


回答1:


nginx alias

    server {
    listen      80;
    server_name  pwta;
    index index.html index.php;
    root html;

    location /testpath/ {
        alias html/test/;
    }
    location ~  ^/testpath/(.+\.php)$ { ### This location block was the solution
        alias html/test/;     
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$1;
        include fastcgi_params;
    }
     location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }



回答2:


Both the alias and root directives are best used with absolute paths. You can use relative paths, but they are relative to the prefix config option used to compile nginx, and are generally not what you want.

You can see this by executing nginx -V and finding the value following --prefix=.

Prove this to yourself by looking at the log, you will find a "no such file" error.



来源:https://stackoverflow.com/questions/10084137/nginx-aliaslocation-directive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!