nginx多域名同IP同80端口配置
http://blog.csdn.net/webnoties/article/details/37597959
vi /etc/nginx/nginx.conf
里面有这2句话:
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
于是我们在这个文件夹下新建多站点配置文件
/etc/nginx/sites-enabled
这里有个默认的default文件,删除!
a1.conf
- server
- {
- listen 80;
- server_name www.a1.com;
- index index.html index.htm index.php;#设定访问的默认首页地址
- root /var/www/a1;#设定网站的资源存放路径
- location / {
- index index.html index.htm index.php;
- if (-f $request_filename/index.html){
- rewrite (.*) $1/index.html break;
- }
- if (-f $request_filename/index.php){
- rewrite (.*) $1/index.php;
- }
- if (!-f $request_filename){
- rewrite (.*) /index.php;
- }
- }
- location ~ \.php$ {
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- include fastcgi_params;
- }
- }
a2.conf同上,只是把域名改成a2
service nginx stop
service nginx start
OK启动好了。
(主要是删除default这个文件,好像哪里做限制了,最后记得本地绑定下hosts)
==================================================
#同时监听相同端口,可以通过匹配server_name 来决定最终匹配哪个server
#server1
server {
listen 80;
#server_name localhost;
location / {
rewrite / http://www.baidu.com;
}
}
#server2
server {
listen 80;
#uc-center.m.szy.net 在本地的hosts文件 解析到 127.0.0.1
server_name uc-center.m.szy.net;
location ~* ^\/(.*)$ {
rewrite ^\/(.*)$ http://www.sina.com;
}
}
例如: 访问 http://uc-center.m.szy.net 不会因为server1的配置,而导致跳转到“百度首页”。
nginx会优先匹配 配置有server_name的server2,最终跳转到"新浪首页"。