如何给Nginx配置添加虚拟主机

By | 2020年6月11日

Nginx是目前很多站长比较青睐的服务器环境,相较于apache,Nginx的性能优于apache;本篇文章就给大家介绍如何在nginx上搭建多站点,及nginx虚拟主机的搭建。

1、准备工作:

大家都知道Nginx有一个主配置文件nginx.conf,此文件位于nginx文件夹中的conf文件夹中,初始的配置如下:

worker_processes  1;

error_log  logs/error.log;
pid        logs/nginx.pid;

events {
    # Max value 16384
    worker_connections  8192;
    # Accept multiple connections
    multi_accept on;
}

# Settings that affect all server blocks
http {
    include php_processes.conf;
    include       mime.types;
    default_type  application/octet-stream;

    access_log  logs/access.log;

    sendfile on;
    
    keepalive_timeout  65;
    ssl_session_timeout 10m;
    ssl_protocols TLSv1.2 TLSv1.1 TLSv1 SSLv3;
    ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AES:RSA+3DES:!ADH:!AECDH:!MD5:!DSS; 
    ssl_prefer_server_ciphers on;
    gzip  on;
    # http server

# Begin HTTP Server
server {
    listen 80; # IPv4
    server_name localhost;
    ## Parametrization using hostname of access and log filenames.
    access_log logs/localhost_access.log;
    error_log logs/localhost_error.log;

    ## Root and index files.
    root html;
    index  index.php index.html index.htm;
    ## If no favicon exists return a 204 (no content error).
    location = /favicon.ico {
        try_files $uri =204;
        log_not_found off;
        access_log off;
    }
        
    ## Don't log robots.txt requests.
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }
    ## Try the requested URI as files before handling it to PHP.
    location / {

        ## Regular PHP processing.
        location ~ \.php$ {
            #try_files  $uri =404;
            fastcgi_pass   php_processes;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        ## Static files
        location ~* \.(?:css|gif|htc|ico|js|jpe?g|png|swf)$ {
            expires max;
            log_not_found off;
            ## No need to bleed constant updates. Send the all shebang in one
            ## fell swoop.
            tcp_nodelay off;
            ## Set the OS file cache.
            open_file_cache max=1000 inactive=120s;
            open_file_cache_valid 45s;
            open_file_cache_min_uses 2;
            open_file_cache_errors off;
        }

        ## Keep a tab on the 'big' static files.
        location ~* ^.+\.(?:ogg|pdf|pptx?)$ {
            expires 30d;
            ## No need to bleed constant updates. Send the all shebang in one
            ## fell swoop.
            tcp_nodelay off;
        }
        } # / location
}
# End HTTP Server
# Begin HTTPS Server
server {
    listen 443 http2 ssl;
    server_name localhost;
    ssl_certificate      cert.pem;
    ssl_certificate_key  key.pem;
    ssl_session_cache   shared:SSL:10m;
    ssl_session_timeout 10m;

    ## Parametrization using hostname of access and log filenames.
    access_log logs/localhost_access.log;
    error_log logs/localhost_error.log;

    ## Root and index files.
    root html;
    index  index.php index.html index.htm;

    ## If no favicon exists return a 204 (no content error).
    location = /favicon.ico {
        try_files $uri =204;
        log_not_found off;
        access_log off;
    }

    ## Don't log robots.txt requests.
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }
    ## Try the requested URI as files before handling it to PHP.
    location / {

        ## Regular PHP processing.
        location ~ \.php$ {
            #try_files  $uri =404;
            fastcgi_pass   php_processes;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        ## Static files are served directly.
        location ~* \.(?:css|gif|htc|ico|js|jpe?g|png|swf)$ {
            expires max;
            log_not_found off;
            ## No need to bleed constant updates. Send the all shebang in one
            ## fell swoop.
            tcp_nodelay off;
            ## Set the OS file cache.
            open_file_cache max=1000 inactive=120s;
            open_file_cache_valid 45s;
            open_file_cache_min_uses 2;
            open_file_cache_errors off;
        }

        ## Keep a tab on the 'big' static files.
        location ~* ^.+\.(?:ogg|pdf|pptx?)$ {
            expires 30d;
            ## No need to bleed constant updates. Send the all shebang in one
            ## fell swoop.
            tcp_nodelay off;
        }
        } # / location
} # End HTTPS Server
include  vhost/*.conf;  #此处是引入虚拟主机文件夹中所有配置文件
}

首先在conf文件夹下创建一个虚拟主机配置文件的文件夹vhost,当然了,你可以创建为其它名称,根据自己的喜好来。

然后如上面主配置文件最下方的include代码所示,引入你的虚拟主机文件夹及文件夹下的所有配置文件。

2、配置虚拟主机配置文件:

第一步都完成后,开始配置你的虚拟主机文件了,首先在vhost文件夹中创建一个站点配置文件;

一般情况虚拟主机配置文件的名称是您需要创建的站点的域名,这样后期修改等操作就比较方便。

接下来就是配置文件内容了:

server
{
    listen 80;
    #listen [::]:80;
    server_name www.1901tv.com 1901tv.com;  #你站点的域名,可以帮定多个域名,使用空格分开
    index index.html index.htm index.php default.html default.htm default.php; #默认首页文件名
    root  D:\Wnmp\html\www.1901tv.com; #这里就是你站点文件放置的文件夹位置
    
    #error_page   404   /404.html;

    # Deny access to PHP files in specific directory
    #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
    location ~ \.php$ {
        try_files  $uri =404;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
    include rewrite/thinkphp.conf;  #引入伪静态文件

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

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

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

    location ~ /\.
    {
        deny all;
    }

    access_log  D:/Wnmp/conf/logs/erp.all-face.com.log;  #日志文件
}

至此,虚拟主机配置文件已经配置完成了,接下来就是重启nginx,然后去你的域名服务商网站后台将你的域名解析至你服务器ip,过一会就可以访问你配置的站点了;如果你想再在你的服务器上创建其他站点,只需要在vhost文件夹中创建对应站点的配置文件,然后在站点文件加下再创建对应的文件夹,按照上面步骤配置就ok了,这样你就可以在一台服务器上创建多个站点了!

发表评论

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