使用nginx实现tomcat高可用:NginxTomcat一份http转换为https请求的珍藏配置
使用nginx实现tomcat高可用:NginxTomcat一份http转换为https请求的珍藏配置
前置条件- 已申请域名包括备案成功
- SSL证书已获得
- Nginx部署方式为源码部署,不会的请参考Linux安装nginx - star-xin - 博客园
- nginx已加载SSL模块,不会的请参考nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:37 - ノGHJ - 博客园
改成自己域名、要转发的http地址
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
client_max_body_size 5000m;
#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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# Tomcat
upstream appServer {
server 127.0.0.1:8080 weight=4;
}
server {
listen 80;
# listen somename:8080;
server_name 域名 ;
rewrite ^(.*) https://$host$1 permanent;
}
server {
listen 443 ssl; # https 443
charset utf-8;
server_name 域名; # host_name of URL
# https
#ssl on;
ssl_certificate 证书地址.pem;
ssl_certificate_key 证书地址.key;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# https Tomcat Tomcat http
proxy_set_header X-Forwarded-Proto $scheme;
# Tomcat
proxy_pass 自己的http请求;
}
}
# another virtual host using mix of IP- name- and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}