Nginx关于使用多个.conf文件进行配置多个虚拟主机server的方法

一、序言

Nginx配置主要在/usr/local/nginx/conf/nginx.conf中,我们可以通过该nginx.conf文件配置成我们想要的效果,但如果存在多个虚拟主机需要配置时,nginx.conf文件就会变得庞大臃肿不好管理,如果我们可以按功能分类在多个.conf文件来配置不同的虚拟主机,这样就会清晰明了。本篇博客就是介绍如何在nginx中使用多个.conf文件进行配置多个虚拟主机server。

二、主要步骤

  1. cd 到/usr/local/nginx/conf,建立文件夹mkdir conf.d,赋予权限chmod 777 conf.d
  2. 进入到conf.d,创建一个conf文件touch test.conf,在该文件中写上server块、location块
    在这里插入图片描述
  3. 进入到/usr/local/nginx/conf/nginx.conf,在http块的最后把conf.d的地址添加上include /usr/local/nginx/conf/conf.d/*.conf
user  root;
worker_processes  2; 
error_log  /usr/local/nginx/logs/error.log;
pid  /usr/local/nginx/logs/nginx.pid;
# 单个进程最大的文件连接数
worker_rlimit_nofile 51200;
# events块配置
events {
    use epoll;
    worker_connections  1024;
}

# http块配置
http {
    include  mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request"'
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;
    sendfile  on;
    keepalive_timeout  65;


   # 反向代理服务指定目标url
    server {
        listen      8899;
        server_name  www.hlong.com;
        location  /{
            proxy_pass http://www.baidu.com;
	
        }
    }
	
    # 正向代理服务指定目标url
    server {	
      # listen  80;
        #server_name  url.test.com;
        location  /{
            root /html;
	    index index.html;
        }
    }
    include /usr/local/nginx/conf.d/*.conf;
}

  1. 这样就相当于在nginx.conf中http块后面加上了test.conf的server块部分

三、测试

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值