nginx 多站点配置方法

关于nginx的多站设置,其实和apache很类似哒。 php

假设咱们已经有两个域名,分别是:www.websuitA.comwww.websuitB.com。而且这两个域名已经映射给了IP192.168.1.1的服务器。 html

一、为咱们的站点建立配置文件 nginx

我是这么作的,在nginx的配置文件conf目录下建立一个专门存放VirtualHost的目录,命名为vhosts_conf,能够把虚拟目录的配置所有放在这里。在里面建立名为vhosts_modoupi_websuitA.conf的配置文件并打开,咱们在这里作配置,往里面写: web

server { apache

listen 80;               #监听的端口号 浏览器

server_name websuitA.com;        #域名 服务器

#access_log logs/host.access.log main; 测试

location / { ui

root X:/wnmp/www/websuitA;    #站点的路径 spa

index default.php index.php index.html index.htm;

#站点的rewrite在这里写

rewrite ^/(\w+)\.html$ /$1.php;

rewrite ^/(\w+)/(\w+)$ /$1/$2.php;

}

#错误页的配置

error_page 404 /error.html;

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

location ~ \.php$ {

root X:/wnmp/www/websuitA;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

location ~ /\.ht {

deny all;

}

}

 

这样就作好了,站点A的配置,一样的方法,作websuitB的配置,这里我命名为vhosts_modoupi_websuitB.conf

server {

     listen 80;               #监听的端口号

     server_name websuitB.com;        #域名

     #access_log logs/host.access.log main;

     location / {

        root X:/wnmp/www/websuitB;    #站点的路径

       index default.php index.php index.html index.htm;

#站点的rewrite在这里写

       rewrite ^/(\w+)\.html$ /$1.php;

       rewrite ^/(\w+)/(\w+)$ /$1/$2.php;

     }

  #错误页的配置

     error_page 404 /error.html;

     error_page 500 502 503 504 /50x.html;

     location = /50x.html {

       root html;

     }

     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

     location ~ \.php$ {

        root X:/wnmp/www/websuitB;

        fastcgi_pass 127.0.0.1:9000;

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        include fastcgi_params;

     }

     location ~ /\.ht {

        deny all;

     }

}

 

这样,两个站点的配置就OK了。 
二、在nginx的主配置文件里,包含这两个站点的配置文件。 
  咱们打开conf目录下的nginx.conf文件,很容易作,只要在http{...}段输入如下代码: 

#包含全部的虚拟主机的配置文件

include X:/wnmp/nginx/conf/vhosts_conf/*.conf;

 

这样,nginx的多站点配置就作好了,怎么样打开浏览器测试一下吧

相关文章
相关标签/搜索