Nginx不喜欢重写

时间:2011-04-06 17:51:28

标签: nginx rewrite

我有一个使用子目录的网站,目前仅在将尾部斜杠添加到网址(“http://www.domain.com/dir/”)时才有效。当没有斜杠时,我得到“无法在服务器domain.com:8080连接”(8080是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;
}

1 个答案:

答案 0 :(得分: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;
}