前文讲解了Nginx的安装方法及配置文件的基础配置
CentOS下Nginx环境的搭建及进阶配置(安装篇)
Nginx配置文件详解
如果我们的域名都配置到nginx.conf中,这样文件会很乱, 影响管理和阅读.所以nginx的include支持文件拆分出来,分成不同的配置文件
本文讲解通过主子配置的方式来实现Nginx的相关配置
切换路径,编辑主配置文件
cd /usr/local/nginx/conf/
vim nginx.conf
主要修改的内容包括: 修改用户、修改服务端口、删除默认服务等
后续所有的服务都在子配置文件中进行配置
user root;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
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;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
include /usr/local/nginx/conf/conf.d/*.conf;
#gzip on;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
}
}
创建存放虚拟主机的目录分别写入每台虚拟主机的配置
mkdir conf.d
vim test.conf
编写子配置文件test.conf文件内容
server {
listen 8080;
server_name a.b.com;
charset utf-8;
location / {
root /home/project/test/dist;
try_files $uri $uri/ /index.html last;
index index.html;
}
location /api/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8000/;
}
}