vue.js项目在nginx上部署:使spring后端记录真实ip地址
一,查看后端的访问日志:
[web@blog logs]$ tail -100 accesslog2021-10-06.log 127.0.0.1 - 0.004 [06/Oct/2021:15:35:20 +0800] GET /home/home HTTP/1.0 200 97 http://store.lhdtest.net/ Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36
因为没有做真实IP的配置,
所以后端的ip记录下来后都是127.0.0.1
说明:刘宏缔的架构森林是一个专注架构的博客,
网站:https://blog.imgtouch.com
本文: https://blog.imgtouch.com/index.php/2023/05/28/vue-js-xiang-mu-zai-nginx-shang-bu-shu-shi-spring-hou-duan/
对应的源码可以访问这里获取: https://github.com/liuhongdi/
或: https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,nginx配置
[root@blog conf.d]# more storeweb.conf server { listen 80; server_name store.lhdtest.net; root /data/store/web/html; index index.html; location /api { rewrite ^/api/(.*)$ /$1 break; proxy_pass http://localhost:10800; proxy_redirect off; 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 X-Ngnix-Proxy true; } location / { try_files $uri $uri/ /index.html; } access_log /data/logs/nginxlogs/store_web.access_log; error_log /data/logs/nginxlogs/store_web.error_log; }
说明:
proxy_set_header X-Real-Ip $remote_addr;
这条命令负责把真实IP传递到后端
三,tomcat的访问日志配置:
application.yml
#server server: port: 10800 error: include-stacktrace: always #内嵌tomcat日志 tomcat: accesslog: buffered: true directory: /data/store/back/logs enabled: true file-date-format: yyyy-MM-dd pattern: '%{X-Real-Ip}i %l %T %t %r %s %b %{Referer}i %{User-Agent}i' prefix: accesslog rename-on-rotate: false request-attributes-enabled: false rotate: true suffix: .log
四,测试效果:
访问页面后查看tomcat的访问日志
[web@blog logs]$ tail accesslog2021-10-08.log 124.200.178.99 - 0.145 [08/Oct/2021:10:52:00 +0800] GET /home/home HTTP/1.0 200 97 http://store.lhdtest.net/ Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36(KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 124.200.178.99 - 0.006 [08/Oct/2021:10:52:02 +0800] GET /home/home HTTP/1.0 200 97 http://store.lhdtest.net/ Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36(KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 124.200.178.99 - 0.004 [08/Oct/2021:10:52:04 +0800] GET /home/home HTTP/1.0 200 97 http://store.lhdtest.net/ Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36(KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 124.200.178.99 - 0.004 [08/Oct/2021:10:52:06 +0800] GET /home/home HTTP/1.0 200 97 http://store.lhdtest.net/ Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36(KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 124.200.178.99 - 0.004 [08/Oct/2021:10:53:27 +0800] GET /home/home HTTP/1.0 200 97 http://store.lhdtest.net/ Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36(KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36
可见已可以记录真实IP
五,查看nginx版本:
[root@centos8 nginx-1.18.0]# /usr/local/soft/nginx-1.18.0/sbin/nginx -v nginx version: nginx/1.18.0
六,查看spring boot版本:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.5.4)