nginx为何两个域名都跳到同一个网站?

diypyh JAVA开发工程师  2015-10-07 05:09:48
一台服务器上运行了2个tomcat,端口分别是8080和8081。用nginx作反向代理,目的是让两个独立域名分别访问两个网站。但是我配置好nginx后,两个域名都跳到同一个网站,哪位大神帮我看看怎么回事,谢谢。
已经测试过nginx-1.7.11和1.9.5两个版本都一样。

配置如下 :


#user nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

gzip on;

upstream web_server1 {
server 127.0.0.1:8080;
}


server {
listen 80;
server_name *.yongshunfeng.com;

location / {
proxy_pass http://web_server1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
break;
}
}

upstream web_server2 {
server 127.0.0.1:8081;
}

server {
listen 80;
server_name *.szysqkj.com;

location / {
root D:/project/order/target/order;
proxy_pass http://web_server2;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
break;
}
}

}



这种配置也试过,问题是一样:


#user nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

gzip on;

server {
listen 80;
server_name *.yongshunfeng.com;

location / {
proxy_pass http://127.0.0.1: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;
}
}

server {
listen 80;
server_name *.szysqkj.com;

location / {
proxy_pass http://127.0.0.1:8081;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

}


...全文
351 点赞 收藏 4
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
兔子托尼啊 2015-10-08
使用nginx ,安装不同的负载均衡算法,会在你配置好的ip服务器之间进行切换。 写一起看看。像这样的 upstream backserver{ ip_hash; server 127.0.0.1:9090 down; (down 表示单前的server暂时不参与负载) server 127.0.0.1:8080 weight=2; (weight 默认为1.weight越大,负载的权重就越大) server 127.0.0.1:6060; server 127.0.0.1:7070 backup; (其它所有的非backup机器down或者忙的时候,请求backup机器) }
回复
DY1201 2015-10-08
不是太懂,只是尝试!
回复
DY1201 2015-10-08


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;

    upstream 127.0.0.1 {
		server 127.0.0.1:8080;
	}
	upstream web_server2 {
		server 127.0.0.1:8081;
	}
	
	server {
		listen       80;
		server_name  *.yongshunfeng.com;
		
		location /  {
			proxy_pass http://web_server1;
			proxy_set_header   Host    $host;
			proxy_set_header   X-Real-IP   $remote_addr;
			proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for; 
			break;
		}
location / web_server2 {
			root D:/project/order/target/order;
			proxy_pass http://web_server2;
			proxy_set_header   Host    $host;
			proxy_set_header   X-Real-IP   $remote_addr;
			proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for; 
			break;
		}
	}

}

http://web_server2;的访问路径为127.0.0.1:80/web_server2
回复
meizhennan 2015-10-08
应该还需要upstream的一些配置
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-10-07 05:09
社区公告
暂无公告