nginx前端,tomcat後端伺服器擷取客戶的真實IP,包括tomcat訪問日誌擷取真實IP的配置

來源:互聯網
上載者:User
在安裝完以nginx+tomcat的WEB伺服器,使用預設的配置,會導致伺服器上的記錄檔,只有nginx日誌能擷取到客戶的真實IP,而tomcat以及上面的JAVA WEB應用均不能正常擷取到真正的IP地址,而僅是LOOP(回還地址127.0.0.1,或者0.0.0.0.0.0.1),會導致存入到資料庫的也是如此,通過以下配置,即可以改善結果。

nginx端設定檔/etc/nginx/conf.d/default.conf

server {    listen       80;    server_name localhost;    location /{                        rewrite ^/web(.*)$ /$1 last;                        proxy_pass http://localhost:8080/web/;                        proxy_cookie_path /web /;#以下三個proxy_set_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;     }    error_page   500 502 503 504  /50x.html;    location = /50x.html {        root   /usr/share/nginx/html;    }}

tomcat日誌設定檔$CATALINA_HOME/conf/server.xml

在host中,修改以下內容

其中X-Real-IP與NGINX中配置的要對應,此變數即是客戶的真實IP,pattern的其它參數,請參考官方:http://tomcat.apache.org/tomcat-7.0-doc/config/valve.html#Access_Logging

添加以下valve



以上就介紹了 nginx前端,tomcat後端伺服器擷取客戶的真實IP,包括tomcat訪問日誌擷取真實IP的配置,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.