lnmp安装ngnix配置多域名或多个网站

2019年10月20日
ngnix多域名和多网站设置
ngnix 配置

默认情况下,ngnix下域名的绑定都是通过ngnix.conf文件来配置的,但是有时候我们有多个域名或子域名的情况下要怎么绑定呢?或者有子域名指定不同的目录的时候我们又应该怎么设置呢? 其实很简单,以下就以我的网站为例,很方便就可以进行配置了,如果你们留意看过lnmp一键安装脚本的代码,就知道它的原理。

/usr/local/nginx/conf/nginx.conf
 

这个是默认的nginx.conf路径,如果你不知道你的nginx安装在哪里,可以通过以下这行代码来查找。

find / -name nginx.conf
 

打开nginx.conf文件里边的内容是这样的。

user  test test;

worker_processes auto;
worker_cpu_affinity auto;

error_log  /opt/wwwlogs/nginx_error.log  crit;

pid        /usr/local/nginx/logs/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;

events
    {
        use epoll;
        worker_connections 51200;
        multi_accept off;
        accept_mutex off;
    }

http
    {
        include       mime.types;
        default_type  application/octet-stream;

        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;

        sendfile on;
        sendfile_max_chunk 512k;
        tcp_nopush on;

        keepalive_timeout 60;

        tcp_nodelay on;

        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;

        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
        gzip_vary on;
        gzip_proxied   expired no-cache no-store private auth;
        gzip_disable   "MSIE [1-6]\.";

        #limit_conn_zone $binary_remote_addr zone=perip:10m;
        ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.

        server_tokens off;
        access_log off;

server
    {
        listen 80 default_server;
        #listen [::]:80 default_server ipv6only=on;
        server_name _;
        index index.html index.htm index.php;
        root  /opt/www/default;

        #error_page   404   /404.html;

        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

        include enable-php.conf;

        location /nginx_status
        {
            stub_status on;
            access_log   off;
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|mp3|mp4|wav)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }

        access_log  /opt/wwwlogs/access.log;
    }
include vhost/*.conf;
}
 

这个配置文件里重点是我们要开启以下这一行

include vhost/*.conf;
 

这一行的意思是读取执行vhost目录下的所有.conf文件,所以我们在vhost目录下新建我们域名的配置文件,可以多个域名一个文件,也可以一个域名一个文件。比如我们就在vhost目录下新建了一个www.myzhenai.com.cn.conf的配置文件。

server
    {
        listen 80;
        listen 443 ssl;
        #listen [::]:80;
        server_name www.myzhenai.com.cn myzhenai.com.cn;
        index index.html index.htm index.php default.html default.htm default.php;
        root  /opt/www/www.myzhenai.com.cn;
        ssl_certificate /usr/local/nginx/cert/www.myzhenai.com.cn.pem;
        ssl_certificate_key /usr/local/nginx/cert/www.myzhenai.com.cn.key;

        include rewrite/wordpress.conf;
        #error_page   404   /404.html;

        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

        include enable-php-pathinfo.conf;

location ~* ^.+\.(gif|jpg|jpeg|png|swf|flv|mp3|mp4|ogg|flav|wav|rar|zip)$ {
        valid_referers none blocked  www.myzhenai.com.cn;
        if ($invalid_referer) {
            return 404;
            break;
                    }
}

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }

        access_log  /opt/wwwlogs/www.myzhenai.com.cn.log;
    }
 

这个新建的配置文件里您需要自行修改,因为有些适用,用一些不适用,例如我开启了ssl,所以开启了ssl模块和443端口,我下边就最重要的一些内容做一下简单说明。

        listen 80;
        listen 443 ssl;
        #listen [::]:80;
        server_name www.myzhenai.com.cn myzhenai.com.cn;
        index index.html index.htm index.php default.html default.htm default.php;
        root  /opt/www/www.myzhenai.com.cn;
 

listen 80 #监听80端口

server_name #后面是你的域名或子域名,可以写多个,但指向都是一个目录。

index # 默认文件,即会在网站根目录第一级中打开这些文件中的一个文件,有多个的话会照顺序来打开。

root #这一行是你要指向的网站根目录

配置完conf配置文件后,用以下命令重启ngnix和php

service ngnix restart
service php-fpm restart
 


sicnature ---------------------------------------------------------------------
Your current IP address is: 18.205.67.119
Your IP address location: 美国弗吉尼亚阿什本
Your IP address country and region: 美国 美国
Your current browser is:
Your current system is:
Original content, please indicate the source:
同福客栈论坛 | 蟒蛇科普海南乡情论坛 | JiaYu Blog
sicnature ---------------------------------------------------------------------
Welcome to reprint. Please indicate the source http://www.myzhenai.com.cn/post/2855.html

没有评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注