nginx配置文件入门,以Python接口为例。
nginx.conf配置全文如下,语句结尾必须有分号。内容分成三大块
- 核心模块配置:基本的全局变量设置。
- 事件驱动配置:性能设置,就是并发数量。
- web接口区域:具体的web端口配置。
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
1. 核心模块配置
user nginx; # 设置用户名
worker_processes auto; # 设置worker数量
error_log /var/log/nginx/error.log notice; # 设置重大事项日志,格式是:关键词 路径 错误级别
pid /var/run/nginx.pid; # nginx主进程的pid存放地址。
日志错误级别:debug | info | notice | warn | error | crit | alert | emerg,生产环境最好是warn以上。
2. 事件驱动配置
events {
worker_connections 1024; # nginx性能设置,tcp连接数量。
}
3. web接口区域(http部分)
全部内容如下,包含三部分:
- 文件类型设置。
- 日志设置。
- 自定义设置。
- 性能设置
- 主机设置,主机设置里包含多个路由。
http {
# 文件类型部分
# 读取nginx支持的文件类型
include /etc/nginx/mime.types;
# 统一将不支持的文件类型设置为该类型
default_type application/octet-stream;
# 日志部分
# 访问记录的日志日志格式,格式是:log_format 格式名称 格式
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 路径 格式名称
access_log /var/log/nginx/access.log main;
# 自定义部分
sendfile on; # 针对静态资源服务器的优化。
tcp_nopush on; # 想客户端发送的数据积累到一定程度时,才会
keepalive_timeout 65; # 请求最长保持时间。
types_hash_max_size 2048; # 散列计算的最大长度,可以减少hash冲突。
# 主机设置
server {
listen 80; # 监听的端口
server_name localhost; # 监听的ip
location / {
proxy_pass http://localhost:8000; # 转发的Gunicorn服务器地址及端口号
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /chat {
proxy_pass http://localhost:8000/chat; # 转发的Gunicorn服务器地址及端口号
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}