简体   繁体   English

Nginx不喜欢重写

[英]Nginx isn't liking rewrite

I have a site that uses subdirectories and currently only works when the trailing slash is added to the URL (" http://www.domain.com/dir/ "). 我有一个使用子目录的站点,当前仅在将斜杠添加到URL(“ http://www.domain.com/dir/ ”)时有效。 When there is no trailing slash, I get "unable to connect at server domain.com:8080" (8080 is the listening port Nginx is set up for). 如果没有斜杠,我将收到“无法在server domain.com:8080连接”(8080是为Nginx设置的侦听端口)。

I've tried adding the rewrite suggested here (and here ), but it results in the "cannot connect" error for the entire virtual host. 我尝试添加此处 (和此处 )建议的重写,但是这会导致整个虚拟主机出现“无法连接”错误。

Is there another way to add the trailing slash that I could try? 还有另一种方法可以添加我可以尝试的斜杠吗? Or, is there a way I can configure it to see the URL as a directory (and thus, look for the index file), regardless of the presence of the trailing slash? 或者,是否有一种方法可以配置它以将URL视为目录(并因此查找索引文件),而不管尾部是否存在斜杠?

Edit 编辑

Nginx.conf: 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 block: 服务器块:

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

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

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: www.inc:

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

Change server {} block to 将服务器{}块更改为

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM