Nginx到不同位置的特殊路径

时间:2019-12-17 11:10:20

标签: nginx

在以下配置中,http://mysite有效,但是http://mysite/test始终未找到。为什么?

server {
        listen       80 ;
        listen       [::]:80 ;
        server_name mysite;
        root /home/developer/www;
        error_log /var/log/nginx/test_error.log;

        client_body_timeout 5s;
        client_header_timeout 5s;
        index index.php index.html index.htm;

        location / {
          root /home/developer/www/mysiste-admin;
          try_files $uri $uri/ /index.php?$args;
        }

        location /test/ {
                root /home/developer/www/mysite-test;
                error_log /var/log/nginx/test_error.log ;
                limit_req zone=http burst=20 delay=8;
                limit_conn addr 10;
                try_files $uri $uri/ /index.php?args ;
                fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
                location ~ /test/.+\.php$ {
                   allow 127.0.0.1;
                   fastcgi_pass unix:/var/run/php5-fpm.sock;
                   include fastcgi_params;
                   #we are directly using the $request_filename as its a single php script
                   fastcgi_param SCRIPT_FILENAME $request_filename;
                }
        }
        location ~ \.php$ {
                limit_req zone=http burst=20 delay=8;
                limit_conn addr 10;
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }


}

1 个答案:

答案 0 :(得分:0)

解决方案是使用别名和正确的try_files指令:

 location /test/ {
            alias /home/developer/www/mysite.test;
            error_log /var/log/nginx/test_error.log ;
            limit_req zone=http burst=20 delay=8;
            limit_conn addr 10;
            try_files $uri $uri /mysite-test/index.php$args ;
 }