1.nginx 负载均衡配置 upstream tomcat_server {
ip_hash; server 127.0.0.1:8081 weight=1; server 127.0.0.1:8082 weight=1; } 2.nginx 配置 server { listen 80; server_name crm.xx.cn www.crm.xx.cn; if ($scheme != https) { # 强制 HTTP 跳转至 HTTPS # host 与 server_name 等价, redirect/permanent 分别为临时跳转/永久跳转 rewrite ^(.*)$ https://$server_name$1 permanent; } } server { listen 443 ssl; ssl on; server_name xx.cn www.xx.cn;location / { #每个请求按照ip的hash结果分配,同一个ip的访客固定访问一个后端服务器,可解决动态网页session共享问题 proxy_next_upstream http_502 http_504 error timeout invalid_header; 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; proxy_http_version 1.1; proxy_connect_timeout 5s; proxy_read_timeout 10800s; proxy_send_timeout 12s; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; client_max_body_size 100m; proxy_pass http://tomcat_server; }location /preview { proxy_next_upstream http_502 http_504 error timeout invalid_header; 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; proxy_http_version 1.1; proxy_connect_timeout 5s; proxy_read_timeout 10800s; proxy_send_timeout 12s; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_pass http://localhost:8080;} #配置Nginx动静分离,定义的静态页面直接从Nginx发布目录读取location ~ /upload/.*\.(png|jpg)$ { #每个请求按照ip的hash结果分配,同一个ip的访客固定访问一个后端服务器,可解决动态网页session共享问题 proxy_next_upstream http_502 http_504 error timeout invalid_header; 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; proxy_http_version 1.1; proxy_connect_timeout 5s; proxy_read_timeout 10800s; proxy_send_timeout 12s; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; client_max_body_size 100m; proxy_pass http://tomcat_server; } location ~ /preview/.*\.(png|jpg)$ { #每个请求按照ip的hash结果分配,同一个ip的访客固定访问一个后端服务器,可解决动态网页session共享问题 proxy_next_upstream http_502 http_504 error timeout invalid_header; 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; proxy_http_version 1.1; proxy_connect_timeout 5s; proxy_read_timeout 10800s; proxy_send_timeout 12s; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_pass http://localhost:8080; } location ~ .*\.(js|css|png|jpg)$ { root html/static; #expires定义用户浏览器缓存的时间为3天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力 if (-f $request_filename) { expires 3d; } } ssl_certificate /home/nginx/crmcert/xxxx.pem; ssl_certificate_key /home/nginx/crmcert/xxxxx.key; ssl_session_timeout 5m; 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; }3 tomcat server.xml 配置找到
然后修改HOST里的value值,我这里修改为 <Host name="xx.cn" appBase="webapps" <!--这里的name要与nginx配置文件里的server_name保持一致--> unpackWARs="true" autoDeploy="true">