修改nginx伺服器配置,在conf檔案下的nginx.conf檔。
<code><span>#user nobody; #运行用户</span><span>#worker_processes 1; #启动进程数,通常设置成和cpu数目相同</span><span>#error_log /var/log; #错误日志</span> events { worker_connections <span>1024</span>;<span>#单个后台work process进程最大并发链接数</span> } <span>#设定http服务器</span> http{ <span>#设定mime类型,类型由mime.type文件定义</span> include /etc/nginx/mime.types; default_type <span>application</span>/octet-stream; <span>#设定日志格式</span> access_log /var/<span>log</span>/nginx/access.<span>log</span>; sendfile <span><span>on</span></span>; <span>#连接超时时间</span><span>#keepalive_timeout 0;</span> keepalive_timeout <span>65</span>; tcp_nodelay <span><span>on</span></span>; <span>#集群中的所有后台服务器的配置信息,用于负载均衡</span> upstream local_tomcat{ server <span>192.168</span><span>.0</span><span>.111</span>:<span>80</span>; server <span>192.168</span><span>.0</span><span>.109</span>:<span>8080</span>; } server{ listen <span>8090</span>; <span>#当前代理服务器监听端口</span> server_name localhost:<span>80</span>; <span>#监听后要转到地址</span><span>#charset koi8-r;</span><span>#access_log logs/host.access.log main;</span> location / { <span>#location表示匹配路径,/表示所有请求</span> root html; <span>#静态文件存放的路径</span> index index.html index.htm; <span>#没有主页时,此为主页,可多个</span> } <span>#error_page 404 /404.html;</span><span># redirect server error pages to the static page/50x.html</span> error_page <span>500</span><span>502</span><span>503</span><span>504</span> /<span>50</span>x.html; location = /<span>50</span>x.html { root html; } } } </code>
常見問題總結:
1. nginx+tomcat影響request值
request.getScheme() //總是 http,而不是實際的http或https
request.isSecure() //總是false(因為總是http)
request.getRemoteAddr() //總是 nginx 請求的 IP,而不是使用者的IP
request.getRequestURL() //總是 nginx 請求的URL 而不是使用者實際請求的 URL
request.getServerName //總是nginx請求的ip
request.getServerPort //總是nginx請求的連接埠
response.sendRedirect( 相對url ) //總是重新導向到 http 上 (因為認為目前是 http 要求)
解決方法:
修改nginx設定:
<code>proxy_<span>set</span>_header Host <span>$host</span>; <span>#解决getRequestURL、getServerName、getServerPort问题</span> proxy_<span>set</span>_header X-Real-IP <span>$remote_addr</span>;<span>#把真实的ip发送给转发的web服务器</span> proxy_<span>set</span>_header X-Forwarded-For <span>$proxy_add_x_forwarded_for</span>;<span>#解决request.getScheme()只能返回http而不是https问题</span></code>
設定tomcat的server.xml的Engine模組,設定一個value
<code><span>< <span>Valve</span><span>className</span>=<span>"org.apache.catalina.valves.RemoteIpValve"</span><span>remoteIpHeader</span>=<span>"X-Forwarded-For"</span><span>protocolHeader</span>=<span>"X-Forwarded-Proto"</span><span>protocolHeaderHttpsValue</span>=<span>"https"</span> /></span></code>
版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。
以上就介紹了Nginx配置總結,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。