本檔案用于指導在CentOS 6.5下使用nginx反向代理tomcat,并在nginx端支持ssl,
安裝nginx,參見CentOS 6 nginx安裝,
SSL證書申請,參見騰訊SSL證書申請和配置或[使用certbot為站點添加https支持],
安裝tomcat,目前使用的版本為8.x,一般使用以下版本:點擊下載8.5.x
nginx 反向代理以及ssl配置
這里僅介紹片段,例子如下,以letsencrypt證書為例(騰訊證書配置參見https://cloud.tencent.com/document/product/400/4143):
upstream tomcat {
server 127.0.0.1:8080 weight=1;
}
server {
server_name xxx.com;
charset utf-8;
location / {
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Connection "";
proxy_set_header X-Forwarded-Proto https;
proxy_redirect off;
client_max_body_size 100m;
client_body_buffer_size 256k;
proxy_connect_timeout 60;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_buffer_size 8k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_pass http://tomcat;
}
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/xxx.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/xxx.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
定義后端服務器,這里未舉例負載均衡,
upstream tomcat {
server 127.0.0.1:8080 weight=1;
}
定義虛擬服務器
這里主要需要注意ssl證書的配置,以及代理部分的header設定,其影響tomcat端獲取真實客戶端ip、請求協議等,在未恰當配置的情況下可能造成獲取的ip是nginx服務器的ip以及造成資源跨域問題,
tomcat配置
在nginx配置ssl證書的情況下,tomcat組態檔中不需要另行配置ssl證書,但需要獲取nginx設定的請求頭等資訊,以下配置中的埠視具體情況配置,
將原本注釋的8080 Connector恢復,并改成如下
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="443"
proxyPort="443"/>
在host標簽內添加如下(如果已存在則忽略)
其中%h用于顯示客戶端ip,但是在代理模式下顯示的是nginx的ip,可考慮將 "%h" 修改為 "%{X-Real-IP}i" 即可獲取到nginx請求頭中已配置的客戶端ip,
<Valve className="org.apache.catalina.valves.AccessLogValve"
directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t %r %s %b" />
獲取真實ip,其中httpsServerPort在nginx使用默認443埠時不需添加,否則應當指定,
<Valve className="org.apache.catalina.valves.RemoteIpValve"
remoteIpHeader="x-forwarded-for"
remoteIpProxiesHeader="x-forwarded-by"
protocolHeader="x-forwarded-proto"
protocolHeaderHttpsValue="https://www.cnblogs.com/MartyCode/p/https" httpsServerPort="7001"
/>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/125299.html
標籤:Linux
上一篇:mysql 5.7.18安裝教程
下一篇:mysql主從備份配置
