apache,nginx反向代理出现内网ip解决方法

星期四, 2012-02-23 | Author: Lee | JAVA-and-J2EE, linux | 11,920 views

apache中设置如下反向代理后端的tomcat出现
request.getServerName();获取到的主机名称就会是localhost,解决这个

<VirtualHost *:80>
    ServerAdmin service@i5a6.com
    DocumentRoot /usr/local/apache/htdocs
    ServerName i5a6.com
    ServerAlias *.i5a6.com
     DirectoryIndex  index.html  index.html.var  index.jsp
     ProxyPass / http://localhost:8080/
     ProxyPassReverse / http://localhost:8080/
     ErrorLog "logs/hscode-error.log"
     CustomLog "logs/hscode-access.log" common
</VirtualHost>

在httpd.conf 文件中增加:
ProxyPreserveHost On 即可

对应的nginx 也有此问题解决之法:
proxy_set_header Host $host;

upstream mytomcat{
 #server 127.0.0.1:8000 weight=3;
 #server 127.0.0.1:8001;
 #server 127.0.0.1:8002;
 server 127.0.0.1:8080;
 }
 
server
{
listen 80;
server_name i5a6.com *.i5a6.com;
index index.html index.htm ;
root /usr/local/data/htdocs/www;
 
location / {
# proxy_set_header Host %host;
# proxy_set_header X-Forwarded-For $remote_addr;
 proxy_pass http://mytomcat;
proxy_set_header Host $host;
 }

Tags: , , , ,

文章作者: Lee

本文地址: https://www.pomelolee.com/919.html

除非注明,Pomelo Lee文章均为原创,转载请以链接形式标明本文地址

No comments yet.

Leave a comment

Search

文章分类

Links

Meta