HTTP到HTTPS重定向,nginx配置导致对HTTPS

时间:2016-01-26 02:27:20

标签: nginx linode

您好我是Nginx的新手,并希望将我的http请求重定向到https。

我在Linode云系统上有两个端口80和444的负载平衡配置。

如果请求来自https,那么在将SSL终止到LB后向我的服务tomcat发送请求时,请加载均衡器。

如果请求来自http,则负载均衡器正在发送到我的nginx服务器,该服务器将请求重定向到https。

我什么时候开始,我启动我的nginx服务器,我看到继续登录我的tomcat服务器的重定向网址甚至没有人打我的http网址。我有以下完整的nginx.conf文件。

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

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

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
            listen       80 ;
            server_name  example.com;
            #return 301 https://$server_name$request_uri;
            rewrite  ^ https://$server_name/$request_uri permanent;
            root         /usr/share/nginx/html;

            # Load configuration files for the default server block.
            include /etc/nginx/default.d/*.conf;

            location =/ {
                 }

            error_page 404 /404.html;
                location = /40x.html {
            }

            error_page 500 502 503 504 /50x.html;
                location = /50x.html {
            }
        }

如果我将IP地址替换为实际域名,则相同的配置可以正常工作。

以下是基于位置的卷曲结果,我从HTTPS位置标题重定向后看到正在显示正确执行的https://example.com/login

# curl -i http://example.com
HTTP/1.1 301 Moved Permanently
Server: nginx/1.6.3
Date: Fri, 29 Jan 2016 07:43:54 GMT
Content-Type: text/html
Content-Length: 184
Connection: close
Location: https://example.com/

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.6.3</center>
</body>
</html>

# curl -i https://example.com
HTTP/1.1 302 Found
Server: Apache-Coyote/1.1
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
Strict-Transport-Security: max-age=31536000 ; includeSubDomains
X-XSS-Protection: 1; mode=block
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
Set-Cookie: JSESSIONID=C3B65BD4E015F05705B585F5F8D70074; Path=/; Secure; HttpOnly
Location: https://example.com/login
Content-Length: 0
Date: Fri, 29 Jan 2016 07:44:03 GMT
Connection: close

#curl -i https://example.com/login
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
Strict-Transport-Security: max-age=31536000 ; includeSubDomains
X-XSS-Protection: 1; mode=block
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
Set-Cookie: JSESSIONID=6B30D6D70672A99F13B2F441B2F2150E; Path=/; Secure; HttpOnly
Content-Type: text/html;charset=ISO-8859-1
Content-Language: en-US
Transfer-Encoding: chunked
Date: Fri, 29 Jan 2016 07:44:18 GMT
Connection: close

<HTML context of login page> 

请建议我在这里缺少什么。

2 个答案:

答案 0 :(得分:1)

要将所有请求重定向到端口80,请使用以下配置。不需要进一步的行,并且可能会跳过服务器的目的:

server {
    listen 80 default_server;
    server_name _;
    rewrite  ^  https://$host$request_uri permanent;
}

这样,无论哪个主机甚至IP地址都将转发到https对应方。如果您确定只有一个目标主机,则可以使用它而不是$host变量(之后不要输入/):

    rewrite ^  https://example.com$request_uri permanent;

如果你使用return

会更好
    return 301 https://example.com$request_uri;
    # or
    # return 301 https://$host$request_uri;

由于这是此server阻止的唯一目的,因此请删除所有其他指令,例如rootlocationerror_pageinclude

请注意/etc/nginx/conf.d/*.conf/etc/nginx/sites-enabled/*.conf处的其他文件,他们可能会覆盖这些设置。

重新加载nginx配置和测试。我建议使用cURL - 这是预期的结果:

$ curl -i http://example.com
HTTP/1.1 301 Moved Permanently
Server: nginx/1.8.0
Date: Wed, 27 Jan 2016 17:33:45 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: https://example.com/

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.8.0</center>
</body>
</html>

查看并复制Location:标题内容,然后使用cURL再次测试(如果您使用自签名证书,请使用-k):

curl -i https://example.com

结果应来自您的tomcat应用程序, NOT 另一个重定向到同一页面。如果结果相同(从日期开始是安全的),那么你的LB可能会将https请求发送回nginx,从而导致循环。

请注意,如果tomcat应用程序不理解它位于代理(LB)后面,它也可能会转发到https。在这种情况下,您需要设置应用程序配置以正确理解这一点(如果是这种情况,请告诉我)。

答案 1 :(得分:0)

我看到我的Nginx服务器在使用端口:80的公共IP上公开,并且它正在接收来自不需要的主机的流量。

由于我在Nginx服务器块中有一个默认配置,它将所有传入的http:80流量从任何主机重定向到https:443上的tomcat,这就是我在tomcat服务器中看到大量日志的原因。

我必须在我的/etc/nginx.conf中添加以下配置以重定向端口:如果请求仅来自我的域,则将80个流量发送到https。

Reflection