在非标准端口上使用 SSL 时无法在 Nginx 中重写 URL

在非标准端口上使用 SSL 时无法在 Nginx 中重写 URL

我在非标准端口上使用应用程序的测试实例,并希望使用 nginx 将所有传入http请求重定向到https

这是我的 nginx 配置:

I tried the above but Cant seem to get it working : please see below 

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

    server {
       listen                       9000;
       server_name                  iossapp1.com;

       if ($scheme = 'http') {
            rewrite ^               https://$server_name:9443$request_uri? permanent;
       }
    }

    ssl_certificate                 /etc/nginx/ssl/server.crt;
    ssl_certificate_key             /etc/nginx/ssl/server.key;

    server {
       listen                            9443 ssl;
       server_name                  iossapp1.com;

      error_page   497             https://$host:9443$request_uri;

       location / {
            proxy_pass              http://127.0.0.1:9001;
            proxy_set_header        Host            $host;
            proxy_set_header        X-Real-IP       $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
       }
    }
}

我参考了以下 URL,但它似乎没有帮助:http://forum.nginx.org/read.php?2,155978,213340#msg-213340

除了这个问题之外:只有当我将表单发布到服务器时才会出现问题,以下是 /var/log/nginx/access.log 文件中的几个示例:

127.0.0.1 - - [07/Jan/2013:17:29:14 +0800] "GET /admin/logout HTTP/1.1" 302 0 "https://iossapp1.com:9443/admin/iosIndex" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11"

127.0.0.1 - - [07/Jan/2013:17:32:45 +0800] "POST /admin/editStaff HTTP/1.1" 302 0 "https://iossapp1.com:9443/admin/editStaff?staff.id=612" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11" 127.0.0.1 - - [07/Jan/2013:17:32:45 +0800] "GET /admin/staffIndex HTTP/1.1" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11"

127.0.0.1 - - [07/Jan/2013:17:35:45 +0800] "POST /admin/authenticate HTTP/1.1" 302 0 "https://iossapp1.com:9443/admin/login" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11" 127.0.0.1 - - [07/Jan/2013:17:35:45 +0800] "GET /admin/masterIndex HTTP/1.1" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11"

相比之下,GET URL 的行为正确如下: 127.0.0.1 - - [07/Jan/2013:17:36:21 +0800] "GET /admin/masterIndex HTTP/1.1" 200 2324 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11"

我注意到响应代码 (302, 304) 后面的数值在重定向失败时为 0,而在重定向成功的情况下,该数值不为零。这个数字代表什么?

答案1

我找到了解决方案:

我的参赛作品 proxy_set_header Host $host:$server_port;

原本缺少$server_port

来源 :https://stackoverflow.com/questions/10168155/nginx-proxy-https-to-http-on-non-standard-port

现在它起作用了!=D

相关内容