nginx 域名绑定

682 阅读1分钟

nginx可绑定多个域名

多个域名规则可以写一个配置文件里

也可以分别建立多个域名配置文件

一、一个域名一个文件的写法

1,首先打开nginx域名配置文件存放目录:/usr/local/nginx/conf/servers
2,如要绑定域名www.ceshi.com 
3,在此目录建一个文件:www.ceshi.com.conf
文件内容:

server{
    listen 80;
    server_name www.ceshi.com; #绑定域名
    index index.htm index.html index.php; #默认文件
    root /home/www/ceshi.com; #网站根目录
    include location.conf; #调用其他规则,也可去除
}

4,重起nginx服务器,域名就绑定成功了nginx服务器重起命令:/etc/init.d/nginx restart

二、一个文件多个域名的写法(二级三级域名)

server{
    listen 80;
    server_name www.ceshi.com; #绑定域名
    index index.htm index.html index.php; #默认文件
    root /home/www/ceshi.com; #网站根目录
    include location.conf; #调用其他规则,也可去除
}

server{
    listen 80;
    server_name wx.ceshi.com; #绑定域名
    index index.htm index.html index.php; #默认文件
    root /home/www/wx.ceshi.com; #网站根目录
    include location.conf; #调用其他规则,也可去除
}

三、域名不带 www 时可实现301跳转 到带www

server{
    listen 80;
    server_name ceshi.com;
    rewrite ^/(.*) http://www.ceshi.com/$1 permanent;
}

四、域名绑定 端口

server {
    listen  80;  
    server_name ceshi.com www.ceshi.com; #绑定域名
    location / {  
         proxy_pass http://127.0.0.1:8000;  #指定端口号 8000
    } 
}