Home  >  Q&A  >  body text

nginx - tomcat configures the specified domain name https under multiple domain names and makes it forcefully redirect

How to make tomcat force one of the domain names to use https in the case of multiple domain names? Looking at many configurations on the Internet, they are all performed under the same domain name.
For example: There are test.emp.com and testadmin.emp.com on a tomcat. Now the content on testadmin.emp.com must be forced to use https access. Don't know how to configure it?
I used nginx to configure port 433 and found that testadmin.emp.com can indeed be accessed using https, but it can also be accessed using http. Then I added a

to the listening port 80.
server {  
  listen      80;  
  server_name    testadmin.emp.com;  
  return      301 https://$server_name$request_uri;  
} 

or add
···

server{  
    server_name  testadmin.emp.com;;  
    listen 80;  
    index index.jsp;  

    if ($host = ' testadmin.emp.com;' ) {
        rewrite ^(.*)$  https:// testadmin.emp.com; permanent;  
    }
    location / {  
             proxy_pass http://127.0.0.1:8080;  
             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;  
    }  
}

···
The redirection loop begins

怪我咯怪我咯2701 days ago1246

reply all(1)I'll reply

  • 黄舟

    黄舟2017-05-27 17:47:07

    No reverse proxy is required, just add <security-constraint> configuration in the web.xml of the site where you want HTTPS. For example

    <security-constraint>
        <display-name>zzz</display-name>
        <web-resource-collection>
          <web-resource-name>xxx</web-resource-name>
          <url-pattern>/xxx/*</url-pattern>  ##指定需要强制https的url
          <http-method>DELETE</http-method> 
          <http-method>GET</http-method>
          <http-method>POST</http-method>
        </web-resource-collection>
        <user-data-constraint>
          <transport-guarantee>CONFIDENTIAL</transport-guarantee>
        </user-data-constraint>
      </security-constraint>
      <context-param>
        <param-name>ca_root</param-name>
        <param-value>/xxx/tomcat.keystore</param-value> ##指定服务器证书ks
      </context-param>

    reply
    0
  • Cancelreply