2011-04-06 52 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