2016-05-13 121 views
3

我有nginx的麻煩。Nginx重寫或內部重定向週期內部重定向

這是我的配置。

server { 
    listen     80; 
    listen     [::]:80; 
    server_name    www.test.local; 
    return     301 http://test.local$request_uri; 
} 
server { 
    server_name    test.local; 
    root     /usr/share/nginx/test/htdocs/web; 

    # error_log    /var/log/nginx/test.error.log; 
    # access_log    /var/log/nginx/test.access.log; 

    rewrite     ^/app\.php/?(.*)$ /$1 permanent; 

    location/{ 
     index app.php; 
     try_files   $uri @rewriteapp; 
    } 

    location @rewriteapp { 
     rewrite ^(.*)$  /app.php/$1 last; 
    } 

    location ~ ^/app\.php(/|$) { 
     fastcgi_pass  172.17.0.1:48000; 
     fastcgi_split_path_info ^(.+\.php)(/.*)$; 

     include    fastcgi_params; 
     fastcgi_param  SCRIPT_FILENAME $realpath_root$fastcgi_script_name; 
     fastcgi_param  DOCUMENT_ROOT $realpath_root; 
     internal; 
    } 

    location /uploads/ { 
     root    /usr/share/nginx/test/htdocs/web/uploads; 
     try_files   $uri $uri/; 
     access_log   off; 
     expires    30d; 
    } 

    location /images/ { 
     root    /usr/share/nginx/test/htdocs/web/images; 
     try_files   $uri $uri/; 
     access_log   off; 
     expires    30d; 
    } 

    location /css/ { 
     root    /usr/share/nginx/test/htdocs/web/css; 
     try_files   $uri $uri/; 
     access_log   off; 
     expires    30d; 
    } 

    location /js/ { 
     root    /usr/share/nginx/test/htdocs/web/js; 
     try_files   $uri $uri/; 
     access_log   off; 
     expires    30d; 
    } 

    location /fonts/ { 
     root    /usr/share/nginx/test/htdocs/web/fonts; 
     try_files   $uri $uri/; 
     access_log   off; 
     expires    30d; 
    } 

    location = /favicon.ico { 
     return    204; 
     access_log   off; 
     log_not_found  off; 
    } 
} 

我想我作爲http://test.local/css/鏈接到/ usr /共享/ nginx的/測試/ htdocs中/風格地點/像我一樣在我的配置。

但是,當IM進入例如http://test.local/css/flow.css即時得到一個錯誤:

2016/05/13 15:55:30 [error] 5#5: *64 rewrite or internal redirection cycle while internally redirecting to "/css/flow.css///////////", client: 192.168.99.1, server: test.local, request: "GET /css/flow.css HTTP/1.1", host: "test.local", referrer: "http://test.local/" 

請告訴我這裏的問題?

回答

3

您的配置有兩個問題。

第一個問題是您的root指令是錯誤的,所以找不到文件。

第二個問題是默認操作是將另一個/添加到URI的末尾。

你需要一個有效的默認操作添加到您的try_files指令,如404響應:

try_files $uri $uri/ =404; 

所以,回到第一個問題。您的配置狀態/css/example.css位於/usr/share/nginx/test/htdocs/web/css/css/example.css。請注意,/css/已被複制。 /images/,/js//fonts/也是如此。有關root指令的詳細信息,請參見this document

在大多數配置中,就足夠了指定rootserver塊級並允許其由幾乎所有location塊被繼承,而不是在每個位置重複它。

您的問題指出應在/usr/share/nginx/test/htdocs/styles/中找到/css/文件。這需要alias指令。有關alias指令的詳細信息,請參見this document。例如:

location /css/ { 
    alias  /usr/share/nginx/test/htdocs/styles/; 
    access_log off; 
    expires  30d; 
}