【问题标题】:SSL not working on nginx 443 configurationSSL 不适用于 nginx 443 配置
【发布时间】:2017-07-03 15:08:24
【问题描述】:

我正在为一个网站设置 nginx。我只想为一些子链接设置 ssl 链接用户登录,注册。我创建一个让我们加密 ssl 并且证书工作正常。我检查了 ssl 购物者。我只想为商店而不是所有站点配置 ssl。所以我将商店从 80 重定向到 443,只有商店想要工作 ssl。但是在我配置之后 在 nginx 上,一些按钮(javascriptvoid)不起作用。它说混合内容,所以当我查看查看源时,它显示按钮的 url 在商店页面中仍然是 http。(它应该是 https)。

我检查了一切,我重新配置了 nginx,检查了 tomcat 端,一切正常。我不知道是什么问题。

我的 nginx 配置在这里为您服务

((/sub)子位置是我想要工作的一个 https)

NGINX 配置

upstream backend_front {
    ip_hash;

    server tomcat_serverip:8080;

}

server {
    listen       80;
    server_name  www.domianname.com;

    charset utf-8;

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

     location / {
    proxy_pass   http://backend_front;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }


    location /sub/ {

        if ($http_x_forwarded_proto != 'https') {
            return       301 https://$server_name$request_uri;
        }

       proxy_pass   http://backend_front;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    }

}

server {
    listen       443 ssl;
    server_name  www.domainname.my;

    ssl on;
    ssl_certificate         /etc/letsencrypt/live/fullch.pem;
    ssl_certificate_key     /etc/letsencrypt/live/privkey.pem;

    ssl_session_cache shared:SSL:20m;
    ssl_session_timeout 15m;

    ssl_prefer_server_ciphers       on;
    ssl_protocols                   TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers                     ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS;


    charset utf-8;
    access_log  /var/log/nginx/443access.log main;

    add_header Strict-Transport-Security "max-age=31536000";


        root /data/resources/;

location /sub/ {

       proxy_pass   http://backend_front;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;


    }

    location / {
        proxy_pass   http://backend_front;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
}


  # for static files caching 
    location ~ .*\.(html|jsp)?$ {
        proxy_pass http://backend_front;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
        root /data/resources/;
        expires 20m;
    }    # for static files caching -- end 

    location ~ /favicon\.ico {
        root html;
    }

    location ~ /\. {
        deny all;
    }
}

按钮的视图源结果

<a href="javascript:void(0)" url="http://www.example.com/store/account.htm" onclick="tiaozhuan(this)" style="padding:0px">Manage Account</a>
<a href="javascript:void(0)" url="http://www.example.com/store/order.htm" onclick="tiaozhuan(this)" style="padding:0px">My Orders</a>

当我单击按钮时,此错误消息显示在 google chrome 元素控制台上(但对于 http,它工作正常。)

jquery-1.8.3.min.js:2 Mixed Content: The page at 'https://www.example.com/store/account.htm' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://www.example.com/isLogin.htm'. This request has been blocked; the content must be served over HTTPS.
send    @   jquery-1.8.3.min.js:2
ajax    @   jquery-1.8.3.min.js:2
tiaozhuan   @   account.htm:155
onclick @   account.htm:77

请帮我解决这个问题。我在过去 1 周一直坚持解决这个问题。我不是程序员,我是系统管理员。和新的 nginx.please 帮助解决这个问题。

【问题讨论】:

标签: http tomcat ssl nginx https


【解决方案1】:

“混合内容”通常是指一个页面,该页面在加载时会在 HTTP 和 HTTPS 下发出二次请求(例如,图像、css、javascript)。

当您使用 HTTPS 发出请求时,所有后续请求也必须是 HTTPS。您需要将按钮的 URL 转换为“https://”

【讨论】:

  • 它是一个来自后端 java 的 webpath 方法------- public static String getURL(HttpServletRequest request) { String contextPath = request.getContextPath().equals("/") ? "" : request.getContextPath(); int port = null2Int(Integer.valueOf(request.getServerPort())); System.out.println(request.getScheme());字符串 url = request.getScheme() + "://" ; url += request.getServerName(); if (port != 80 && port != 443) { url += ":" + port;网址 += 上下文路径; } else { url = url + contextPath; } 返回网址; }
  • 如果用于生成返回 URL 的请求的方案是“http”而不是“https”,那么您将收到错误消息。如果您管理此代码,则需要检查以在端口为 443 时强制方案为“https”。
  • 你能解释一下如何从 nginx 服务器块中做到这一点吗?这将是一个很大的帮助。
  • 我以为我之前的回复做到了?应修改您列出的 Java/C# 以解决问题,(如果我理解问题)。
【解决方案2】:

最后我们发现了问题并在 3 周后解决了问题。 问题是因为代理 ip。我们使用 nginx 服务器进行重定向和代理。所以我们需要在tomcat的server.xml中添加关于nginx服务器ip的附加实体。这里是条目。

<Valve className="org.apache.catalina.valves.RemoteIpValve" internalProxies="111\.111\.111\.111" remoteIpHeader="X-Forwarded-For" protocolHeader="X-Forwarded-Proto" protocolHeaderHttpsValue="https" httpsServerPort="443" />

所以内部代理是 Nginx IP。

【讨论】:

    猜你喜欢
    • 2018-07-05
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    • 1970-01-01
    • 2015-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多