【问题标题】:Nginx port_in_redirect not working?Nginx port_in_redirect 不起作用?
【发布时间】:2011-12-23 00:41:39
【问题描述】:

我最近设置了一个 Nginx 服务器来托管 wordpress 安装,并在服务器前使用 Varnish 作为反向代理。 Varnish 在 80 端口上运行,所以我设置了 Nginx 来监听 80 并重定向。不幸的是,在重定向时,端口 8080 被附加到 nginx 请求中。

包括port_in_redirect off 似乎是解决此问题的一般方法,但它似乎对我不起作用。我在下面附上了/sites-enabled/default 配置。我做错什么了吗?! php 重定向似乎工作正常,它只是在location /失败的地方。

/sites-enabled/default:

server { 
    listen 8080 default;
    server_name "" xxx.xxx.xxx.xxx;  #just using IP here (no domain yet)

    port_in_redirect off;
    server_name_in_redirect off;

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


    location / {
            root   /var/www/site/html/;
            index  index index.php;
            try_files $uri/ $uri /index.php?q=$uri&$args;
    }

    location ~ .php$ {
            fastcgi_split_path_info ^(.+.php)(.*)$;
            fastcgi_pass   backend;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www/site/html/$fastcgi_script_name;
            include fastcgi_params;
            fastcgi_param  QUERY_STRING     $query_string;
            fastcgi_param  REQUEST_METHOD   $request_method;
            fastcgi_param  CONTENT_TYPE     $content_type;
            fastcgi_param  CONTENT_LENGTH   $content_length;
            fastcgi_intercept_errors        on;
            fastcgi_ignore_client_abort     off;
            fastcgi_connect_timeout 60;
            fastcgi_send_timeout 180;
            fastcgi_read_timeout 180;
            fastcgi_buffer_size 128k;
            fastcgi_buffers 4 256k;
            fastcgi_busy_buffers_size 256k;
            fastcgi_temp_file_write_size 256k;
            fastcgi_param SERVER_PORT 80;
    }

    location ~ /.ht {
        deny  all;
    }
    location ~ /.git {
        deny  all;
    }
    location ~ /.svn {
        deny  all;
    }

}

upstream backend {
    server 127.0.0.1:9000;
}

nginx.conf:

user www-data;
worker_processes  1;

error_log  /var/log/nginx/error.log debug;
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     off;

    keepalive_timeout  30;
    tcp_nodelay        on;

    gzip  on;
    gzip_proxied any;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

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

nginx:nginx版本:nginx/1.0.9

【问题讨论】:

    标签: nginx varnish


    【解决方案1】:

    其实只是整理了一下。简单的错误 - 在 Wordpress 管理站点中 wp-admin > Settings > WordPress address (URL)Site address (URL) 在 URL 中都有端口 8080!因此重定向。上面的设置像这样可以正常工作。

    如果这不能解决您的问题,我学到的一个技巧是在您的 index.php 中添加 print_r($_SERVER["SERVER_PORT"]); 以确保您从 fastcgi 获得正确的端口(在我的情况下为 80)。

    希望这个简单的步骤可以节省一些时间!

    【讨论】:

      【解决方案2】:

      另一个解决方案是让您的 Nginx 服务器块在您的 localhost IP 上侦听,127.0.0.1 或 [::1](如果您有 IPv6 - 也选择 IPv4)在端口 80 或 8080 上,然后有Varnish 在您用于 Nginx 服务器块的同一端口上侦听外部 IP、xx.xx.xx.xx 或 [::] 上的外部世界。然后你就不必重定向或推迟或任何东西。

      【讨论】:

        猜你喜欢
        • 2015-05-05
        • 2017-01-02
        • 2015-04-09
        • 1970-01-01
        • 2018-01-14
        • 2015-05-02
        • 2017-11-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多