2011-04-06 33 views
1

我有一個使用子目錄的站點,並且當前只有當URL中添加了斜線時才起作用(「http://www.domain.com/dir/」)。當沒有結尾的斜槓時,我得到「無法連接到服務器domain.com:8080」(8080是Nginx設置的監聽端口)。Nginx不喜歡重寫

我嘗試添加重寫建議here(和here),但它導致整個虛擬主機的「無法連接」錯誤。

是否有另一種方法來添加我可以嘗試的尾部斜線?或者,有沒有一種方法可以將它配置爲將URL看作目錄(並因此查找索引文件),而不管是否存在尾部斜槓?

編輯

Nginx.conf:

user www-data; 
worker_processes 4; 

error_log /var/log/nginx/error.log; 
pid  /var/run/nginx.pid; 

events { 
    worker_connections 1024; 
    # multi_accept on; 
} 

http { 
    include  /etc/nginx/mime.types; 

    access_log /var/log/nginx/access.log; 

    sendfile  on; 
    tcp_nopush  on; 
    tcp_nodelay  on; 

    gzip on; 
    gzip_disable "MSIE [1-6]\.(?!.*SV1)"; 

    map $scheme $fastcgi_https { ## Detect when HTTPS is used 
     default off; 
     https on; 
    } 

    include /etc/nginx/conf.d/*.conf; 
    include /etc/nginx/sites-enabled/*; 
} 

服務器塊:

server { 
     listen 8080; 
     server_name domain.com www.domain.com; 
     include www.inc; 

     root /var/vhosts/domain/current/frontend/; 
     include php.inc; 
} 

Php.inc:

index index.php; 

location ~ \.php$ { 
    fastcgi_pass 127.0.0.1:9000; 
    fastcgi_index index.php; 
    #fastcgi_param ENVIRONMENT production; 
    fastcgi_param HTTPS $fastcgi_https; 
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
    #fastcgi_intercept_errors on; 
    fastcgi_connect_timeout 10; 
    fastcgi_send_timeout 15; 
    fastcgi_read_timeout 120; 
    fastcgi_buffer_size 128k; 
    fastcgi_buffers 4 256k; 
    fastcgi_busy_buffers_size 256k; 
    fastcgi_temp_file_write_size 256k; 
    include fastcgi_params; 
} 

www.inc:

if ($host ~ ^([^\.]+\.com)) { 
    rewrite ^/(.*)$ http://www.$host/$1 permanent; 
} 
+0

請添加服務器{} config其中重寫不起作用。 – 2011-05-03 20:18:08

+0

已添加,以及包含。我只是想 - 是www重寫問題?如果是這樣,我可以修改它在那裏添加斜線,但它是否適用於已經包含www的URL?有沒有更好的方法來修改它?我需要www檢查。 – Shauna 2011-05-03 20:31:08

回答

1

更改服務器{}塊

server { 
    listen 8080; 
    port_in_redirect off; 
    server_name www.domain.com domain.com; #Order matters! 
    include www.inc; 

    root /var/vhosts/domain/current/frontend/; 
    include php.inc; 
} 
+0

還沒有。我嘗試編輯重寫行本身,因爲服務器託管了許多不同的站點(所以我必須小心)。我也嘗試用您的建議替換網站中的包含電話本身,但仍然沒有任何提示。如果URL類似於「http://www.domain.com/dir」,您的建議甚至可以工作嗎?在我看來,它只適用於「domain.com/dir」。 – Shauna 2011-05-04 19:02:35

+0

@Shauna,我終於明白了。您的服務器在非標準端口上進行偵聽,因此重定向必須包含$ server_port。查看更新版本。 – 2011-05-04 19:05:47

+0

沒有。 「無法在domain.com:8080建立連接」。用「默認」(即 - 我用於其他站點的)設置,它已經到端口8080,FWIW。 – Shauna 2011-05-04 19:12:56