一、序言
Nginx配置主要在/usr/local/nginx/conf/nginx.conf中,我们可以通过该nginx.conf文件配置成我们想要的效果,但如果存在多个虚拟主机需要配置时,nginx.conf文件就会变得庞大臃肿不好管理,如果我们可以按功能分类在多个.conf文件来配置不同的虚拟主机,这样就会清晰明了。本篇博客就是介绍如何在nginx中使用多个.conf文件进行配置多个虚拟主机server。
二、主要步骤
- cd 到
/usr/local/nginx/conf
,建立文件夹mkdir conf.d
,赋予权限chmod 777 conf.d
- 进入到conf.d,创建一个conf文件
touch test.conf
,在该文件中写上server块、location块
- 进入到
/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;
}
- 这样就相当于在nginx.conf中http块后面加上了test.conf的server块部分