ngnix 根据不同域名转发不同服务
场景:
我只有一台阿里云服务器,然后搭建了两个网站A,B
有两个域名:www.yunmasoft.com ,www.yhskyc.com
目的:
(1)访问两个域名时都是访问同一台主机
(2)www.yunmasoft.com 访问网站A;
www.yhskyc.com访问的是网站B
使用nginx 如何实现呢?
期望:
http://www.yunmasoft.com/ 访问 tomcat:/home/whuang/software/apache/apache-tomcat-7.0.53
http://www.yhskyc.com/ 访问 tomcat:/home/whuang/software/apache/tomcat-7.0.53_yh
两个域名访问的是不同的tomcat 服务
nginx配置步骤:
步骤一:安装nginx
请自行谷歌
步骤二:创建映射文件
创建目录:/usr/local/nginx-1.7.8/vhosts
在/usr/local/nginx-1.7.8/vhosts 中创建两个文件:
yhskyc.com.conf yunmasoft.com.conf
yhskyc.com.conf 内容为:
server {
listen 80;
server_name yhskyc.com www.yhskyc.com;
location / {
proxy_pass http://182.92.97.72:8084;
# proxy_redirect off ;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
}
yunmasoft.com.conf 内容如下:
server {
listen 80;
server_name yunmasoft.com www.yunmasoft.com;
location / {
proxy_pass http://182.92.97.72:8083;
# proxy_redirect on ;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
}
步骤三:把这两个文件包含到nginx主配置文件
修改/conf/nginx.conf
在html节点添加
include /usr/local/nginx-1.7.8/vhosts/*;
/conf/nginx.conf 内容如下:
- #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 localhost;
- #charset koi8-r;
- #access_log logs/host.access.log main;
- location / {
- add_header Access-Control-Allow-Origin *;
- root /var/www/html;
- index index.html index.htm;
- }
- #error_page 404 /404.html;
- # redirect server error pages to the static page /50x.html
- #
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- }
- include /usr/local/nginx-1.7.8/vhosts/*;
- }
参考:http://blog.csdn.net/zacklin/article/details/7859680