cool-farmer-programmer

Nginx虚拟主机配置示例

参考文档

单个域名配置

server {
    listen       80;
    server_name  www.t1.com;
    location / {
        root   html/www.t1.com;
        index  index.html index.htm;
    }
}

基于多个域名的虚拟主机配置

       在整体格式上,新增域名虚拟主机的配置和前文配置过的www.t1.com域名的虚拟主机是一样的,区别就是server_name和root参数的配置不同,此时基于3个域名的完整nginx.conf配置文件的内容如下:

# www.t1.com
server {
    listen       80;
    server_name  www.t1.com;
    location / {
        root   html/www.t1.com;
        index  index.html index.htm;
    }
}

# www.t2.com
server {
    listen       80;
    server_name  www.t2.com;
    location / {
        root   html/www.t2.com;
        index  index.html index.htm;
    }
}

# www.t3.com
server {
    listen       80;
    server_name  www.t3.com;
    location / {
        root   html/www.t3.com;
        index  index.html index.htm;
    }
}

基于端口的虚拟主机配置

如果要配置基于端口的虚拟主机,就需要为每个虚拟主机配置不同的端口,端口的范围一般是大于1024小于65535的任意端口,修改后文件如下:

# www.t1.com
server {
    listen       81;
    server_name  www.t1.com;
    location / {
        root   html/www.t1.com;
        index  index.html index.htm;
    }
}

# www.t2.com
server {
    listen       82;
    server_name  www.t2.com;
    location / {
        root   html/www.t2.com;
        index  index.html index.htm;
    }
}

# www.t3.com
server {
    listen       83;
    server_name  www.t3.com;
    location / {
        root   html/www.t3.com;
        index  index.html index.htm;
    }
}

基于IP的虚拟主机配置

# www.t1.com
server {
    listen       192.168.213.10:81;
    server_name  www.t1.com;
    location / {
        root   html/www.t1.com;
        index  index.html index.htm;
    }
}

# www.t2.com
server {
    listen       192.168.213.11:82;
    server_name  www.t2.com;
    location / {
        root   html/www.t2.com;
        index  index.html index.htm;
    }
}

# www.t3.com
server {
    listen       192.168.213.12:83;
    server_name  www.t3.com;
    location / {
        root   html/www.t3.com;
        index  index.html index.htm;
    }
}

HTTPS配置

server {
        listen 443 ssl;
        server_name cns.xiudream.com;
        ssl_certificate ssl/2009162__xiudream.com.pem;
        ssl_certificate_key ssl/2009162__xiudream.com.key;
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        #禁止在header中出现服务器版本,防止黑客利用版本漏洞攻击
        server_tokens off;
        #如果是全站 HTTPS 并且不考虑 HTTP 的话,可以加入 HSTS 告诉你的浏览器本网站全站加密,并且强制用 HTTPS 访问
        fastcgi_param   HTTPS               on;
        fastcgi_param   HTTP_SCHEME         https;
        #access_log /usr/local/nginx/logs/httpsaccess.log;
        
        location / {
            root   /home/www/crm_admin/front;
            index  index.html index.htm;
        }

        #禁止访问的文件或目录
        location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md) {
          return 404;
        }

        access_log  /var/log/crm_admin/service/info.log;
        error_log  /var/log/crm_admin/service.error.log;
}

 

规范优化Nginx配置文件

Nginx的主配置文件是nginx.conf,主配置文件包含的所有虚拟主机的子配置文件会统一放入extra目录中,虚拟主机的配置文件按照网站的域名或功能取名,例如www.qq.com、www.taobao.com。

将配置文件分散到extra目录中

www.t1.com

server {
    listen       192.168.213.10:81;
    server_name  www.t1.com;
    location / {
        root   html/www.t1.com;
        index  index.html index.htm;
    }
}

www.t2.com

server {
    listen       192.168.213.11:82;
    server_name  www.t2.com;
    location / {
        root   html/www.t2.com;
        index  index.html index.htm;
    }
}

www.t3.com

server {
    listen       192.168.213.12:83;
    server_name  www.t3.com;
    location / {
        root   html/www.t3.com;
        index  index.html index.htm;
    }
}

主配置文件nginx.conf

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    # 加载子配置文件
    include extra/www.t1.com.conf;
    include extra/www.t2.com.conf;
    include extra/www.t3.com.conf;
}

 

发表于 2019-08-20 15:11  帅哥是个码农  阅读(181)  评论(0编辑  收藏  举报
 

分类:

技术点:

相关文章: