nginx + tomcat配置

nginx使用反向代理将url转移给tomcat的8080端口。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
server {
listen 80;
server_name www.domain.com;
location / {
root html;
index index.html index.htm;
proxy_pass http://localhost:8080/;
# 下面的配置将用户访问的url传递给tomcat,
# 否则传递给tomcat的只是localhost:8080了
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
server { listen 80; server_name www.domain.com; location / { root html; index index.html index.htm; proxy_pass http://localhost:8080/; # 下面的配置将用户访问的url传递给tomcat, # 否则传递给tomcat的只是localhost:8080了 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }
server {
	listen 80;
	server_name www.domain.com;

	location / {
		root	html;
		index	index.html index.htm;
		proxy_pass	http://localhost:8080/;
                
                # 下面的配置将用户访问的url传递给tomcat,
                # 否则传递给tomcat的只是localhost:8080了
		proxy_set_header Host               $host;
   		proxy_set_header X-Real-IP          $remote_addr;
   		proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
		proxy_set_header X-Forwarded-Proto  $scheme;
	}
}

 

注意:由于使用了nginx做反向代理,所以在代码中request.getRemoteAddr()得到的不再是真实的用户ip,而是nginx服务器的ip(本机的话即为127.0.0.1)。参考这篇文章http://www.iteye.com/topic/1124492

 

Leave a Comment

Your email address will not be published. Required fields are marked *