Nginx (https) reverse proxy tomcat (http) Implementation tutorial, nginxtomcat

Source: Internet
Author: User

Nginx (https) reverse proxy tomcat (http) Implementation tutorial, nginxtomcat

Both nginx and tomcat are configured

The nginx. conf configuration is as follows:

Focus: proxy_set_header X-Forwarded-Proto $ scheme. In addition, because ngrok also requires port 80, the new ip address is bound to different ip addresses. User nginx; worker_processes auto; error_log/var/log/nginx/error. log; pid/run/nginx. pid; # Load dynamic modules. see/usr/share/nginx/README. dynamic. include/usr/share/nginx/modules /*. conf; events {worker_connections 1024 ;} http {log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request" ''$ status $ response" $ http_referer "'' "$ http_user_agent" "$ http_x_forwarded_for" $ "host "'; access_log/var/log/nginx/access. log main; # access_log off; 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; charset UTF-8; # 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; upstream tomcat {server 127.0.0.1: 8080 ;}# resolver 8.8.8.8; server {listen 172.31.xx.228: 80; server_name *. ngrok. bob. kim; # Load configuration files for the default server block. include/etc/nginx/default. d /*. conf; location/{proxy_set_header Host $ host; proxy_set_header X-Real-IP $ remote_addr; proxy_set_header X-Forwarded-For $ scheme; proxy_set_header X-Nginx-Proxy true; proxy_set_header Connection ""; Limit 50 m; Limit 128 k; proxy_connect_timeout 90; proxy_read_timeout 90; limit 4 k; proxy_buffers 6 128 k; Limit 256 k; Limit 256 k; proxy_redirect off; proxy_pass http://172.31.xx.229:80 ;}} Server {listen 172.31.xx.228: 80; server_name bob. kim; rewrite ^ (. *) https: // $ server_name $1 permanent;} # Settings for a TLS enabled server. server {listen 172.31.xx.228: 443; server_name bob. kim; ssl on; ssl_certificate "/etc/pki/nginx/bobkim. pem "; ssl_certificate_key"/etc/pki/nginx/private/bobkim. key "; ssl_session_timeout 5 m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256: ECDHE: ECDH: AES: HIGH :! NULL :! ANULL :! MD5 :! ADH :! RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; # Load configuration files for the default server block. include/etc/nginx/default. d /*. conf; location/{proxy_set_header Host $ host; proxy_set_header X-Real-IP $ remote_addr; proxy_set_header X-Forwarded-For $ scheme; proxy_set_header X-Forwarded-Proto $ scheme; listen X-Scheme $ scheme; proxy_set_header X-SSL-Protocol $ ssl_protocol; proxy_set_header X-HTTPS-Protocol $ ssl_protocol; proxy_set_header X-Nginx-Proxy true; proxy_set_header Connection ""; proxy_redirect off; proxy_pass http://tomcat ; }}} Tomcat server. add the following xml configuration at the end: <Valve className = "org. apache. catalina. valves. remoteIpValve "remoteIpHeader =" x-forwarded-for "remoteIpProxiesHeader =" x-forwarded-by "protocolHeader =" x-forwarded-proto "/> complete configuration <? Xml version = '1. 0' encoding = 'utf-8'?> <! -- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. see the NOTICE file distributed with this work for additional information regarding copyright ownership. the ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file License t in compliance with the License. you may obtain a copy of the License http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "as is" BASIS, without warranties or conditions of any kind, either express or implied. see the License for the specific language governing permissions and limitations under the License. --> <! -- Note: A "Server" is not itself a "Container", so you may not define subcomponents such as "Valves" at this level. documentation at/docs/config/server.html --> <Server port = "8005" shutdown = "SHUTDOWN"> <! -- APR library loader. Documentation at/docs/apr.html --> <Listener className = "org. apache. catalina. core. AprLifecycleListener" SSLEngine = "on"/> <! -- Initialize Jasper prior to webapps are loaded. Documentation at/docs/jasper-howto.html --> <Listener className = "org. apache. catalina. core. JasperListener"/> <! -- Prevent memory leaks due to use of particle java/javax APIs --> <Listener className = "org. apache. catalina. core. JreMemoryLeakPreventionListener"/> <! -- JMX Support for the Tomcat server. documentation at/docs/non-existent.html --> <Listener className = "org. apache. catalina. mbeans. serverLifecycleListener "/> <Listener className =" org. apache. catalina. mbeans. globalResourcesLifecycleListener "/> <! -- Global JNDI resources Documentation at/docs/jndi-resources-howto.html --> <GlobalNamingResources> <! -- Editable user database that can also be used by UserDatabaseRealm to authenticate users --> <Resource name = "UserDatabase" auth = "Container" type = "org. apache. catalina. userDatabase "description =" User database that can be updated and saved "factory =" org. apache. catalina. users. memoryUserDatabaseFactory "pathname =" conf/tomcat-users.xml "/> </GlobalNamingResources> <! -- A "Service" is a collection of one or more "Connectors" that share a single "Container" Note: A "Service" is not itself a "Container ", so you may not define subcomponents such as "Valves" at this level. documentation at/docs/config/service.html --> <Service name = "Catalina"> <! -- The connectors can use a shared executor, you can define one or more named thread pools --> <! -- <Executor name = "tomcatThreadPool" namePrefix = "catalina-exec-" maxThreads = "150" minSpareThreads = "4"/> --> <! -- A "Connector" represents an endpoint by which requests are already ed and responses are returned. documentation at: Java HTTP Connector:/docs/config/http.html (blocking & non-blocking) Java AJP Connector:/docs/config/ajp.html APR (HTTP/AJP) Connector: /docs/apr.html Define a non-ssl http/1.1 Connector on port 8080 --> <Connector URIEncoding = "UTF-8" port = "8080" protocol = "HTTP/1.1" connectionTime Out = "20000" redirectPort = "443"/> <! -- A "Connector" using the shared thread pool --> <! -- <Connector executor = "tomcatThreadPool" port = "8080" protocol = "HTTP/1.1" connectionTimeout = "20000" redirectPort = "8443"/> --> <! -- Define a ssl http/1.1 Connector on port 8443 This connector uses the JSSE configuration, when using APR, the connector shocould be using the OpenSSL style configuration described in the APR documentation --> <! -- <Connector port = "8443" protocol = "HTTP/1.1" SSLEnabled = "true" maxThreads = "150" scheme = "https" secure = "true" clientAuth = "false" sslProtocol = "TLS"/> --> <! -- Define an AJP 1.3 Connector on port 8009 --> <Connector port = "8009" protocol = "AJP/1.3" redirectPort = "8443"/> <! -- An Engine represents the entry point (within Catalina) that processes every request. the Engine implementation for Tomcat stand alone analyzes the HTTP headers encoded with the request, and passes them on to the appropriate Host (virtual host ). documentation at/docs/config/engine.html --> <! -- You shoshould set jvmRoute to support load-balancing via AJP ie: <Engine name = "Catalina" defaultHost = "localhost" jvmRoute = "jvm1"> --> <Engine name = "Catalina" defaultHost = "localhost"> <! -- For clustering, please take a look at documentation at:/docs/cluster-howto.html (simple how to)/docs/config/cluster.html (reference documentation) --> <! -- <Cluster className = "org. apache. catalina. ha. tcp. SimpleTcpCluster"/> --> <! -- The request dumper valve dumps useful debugging information about the request and response data stored Ed and sent by Tomcat. Documentation at:/docs/config/valve.html --> <! -- <Valve className = "org. apache. catalina. valves. RequestDumperValve"/> --> <! -- This Realm uses the UserDatabase configured in the global JNDI resources under the key "UserDatabase ". any edits that are stored med against this UserDatabase are immediately available for use by the Realm. --> <Realm className = "org. apache. catalina. realm. userDatabaseRealm "resourceName =" UserDatabase "/> <! -- Define the default virtual host Note: XML Schema validation will not work with Xerces 2.2. --> <Host name = "localhost" appBase = "webapps" unpackWARs = "true" autoDeploy = "true" xmlValidation = "false" xmlNamespaceAware = "false"> <! -- SingleSignOn valve, share authentication between web applications Documentation at:/docs/config/valve.html --> <! -- <Valve className = "org. apache. catalina. authenticator. SingleSignOn"/> --> <! -- Access log processes all example. Documentation at:/docs/config/valve.html --> <! -- <Valve className = "org. apache. catalina. valves. accessLogValve "directory =" logs "prefix =" localhost_access_log. "suffix = ". txt "pattern =" common "resolveHosts =" false "/> --> <Valve className =" org. apache. catalina. valves. remoteIpValve "remoteIpHeader =" x-forwarded-for "remoteIpProxiesHeader =" x-forwarded-by "protocolHeader =" x-forwarded-proto "/> </Host> </Engine> </Service> </Server>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.