版权声明:From Lay https://blog.csdn.net/Sadlay/article/details/83105066
Nginx安装
关于Nginx在服务器下的安装,请参考之前的博客 Linux服务器nginx安装
tomcat配置
springboot项目直接配置application.properties
文件或者yaml
文件
server.tomcat.remote_ip_header=x-forwarded-for
server.tomcat.protocol_header=x-forwarded-proto
server.tomcat.port-header=X-Forwarded-Port
server.use-forward-headers=true
该配置将指示tomcat从HTTP头信息中去获取协议信息(而非从HttpServletRequest中获取),同时,如果你的应用还用到了spring-security则也无需再配置。
此外,由于spring-boot足够自动化,你也可以把上面四行变为两行:
server.tomcat.remote_ip_header=x-forwarded-for
server.use-forward-headers=true
#或者
server.tomcat.protocol_header=x-forwarded-proto
server.use-forward-headers=true
指定项目名
context-path=mytest
Nginx配置
server {
listen 80;
server_name testgmq.com; #域名
index myindex.html; #指定的server的root的访问页面
root /home/www/index; #指定的server的root目录
#charset koi8-r;
#access_log logs/host.access.log main;
#我工程的context-path=mytest
location /mytest {
proxy_pass http://localhost:8080;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
# root html;
# index index.html index.htm;
}
}
说明
1、nginx允许一个server同时支持http和https两种协议。这里我们分别定义了http:80和https:443两个协议和端口号。如果你不需要http:80则可删除那行。
2、nginx收到请求后将通过http协议转发给tomcat。由于nginx和tomcat在同一台机里因此nginx和tomcat之间无需使用https协议。
3、由于对tomcat而言收到的是普通的http请求,因此当tomcat里的应用发生转向请求时将转向为http而非https,为此我们需要告诉tomcat已被https代理,方法是增加X-Forwared-Proto和X-Forwarded-Port两个HTTP头信息。
更多配置
关于Nginx的更多详细配置,请参考博客 Nginx配置详解