在前几篇文章中,我们已经成功安装好了Nginx,现在我们需要做的是修改Nginx的配置,让它能够反向代理我们的项目。
(1):首先我们应该找到我们的Nginx的配置文件所处路径,如果你是使用
sudo apt install nginx 直接安装Nginx的话,那么你的配置文件在 /etc/nginx下。
因为我是编译安装的(安装过程看这:编译安装Nginx),所以我的配置文件在/usr/local/nginx下进入此路径。
(2):打开并修改配置文件:vim nginx.conf
配置文件如下:
user root; # 设置启动 Nginx 的用户
worker_processes 2; # 工作进程的数量,与 CPU 核心数保持一致
worker_cpu_affinity 01 10; # 工作进程与 CPU 核心绑定
pid /var/log/nginx/nginx.pid;
events {
use epoll; # 使用 epoll 来处理IO事件
worker_connections 20480; # 限定单个进程的最大连接数
}
http {
include mime.types;
default_type application/octet-stream;
# 定义日志格式
log_format main '$time_local $remote_addr $status $request_time '
'$request [$body_bytes_sent/$bytes_sent] '
'"$http_user_agent" "$http_referer"';
# 定义全局请求日志和错误日志的文件路径
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log warn;
# 性能相关配置
sendfile on; # 优化 TCP 性能
tcp_nopush on; # 优化 TCP 性能
keepalive_timeout 65; # KeepAlive 超时时间
gzip on; # 启用 GZip 压缩, 开启后会对所有 Response 报文进行压缩
# 负载均衡配置
# upstream app_server {
# server 127.0.0.1:9000 weight=6;
# }
server {
listen 80; # 绑定的端口号
server_name aaa.bbb.cn; # 绑定的域名或IP地址
# 当前 Server 的日志设置,xxx代表你的项目名称
access_log /opt/xxx/logs/ngx_access.log main;
error_log /opt/xxx/logs/ngx_error.log;
# 网站 Logo 图标配置
# location = /favicon.ico {
# empty_gif;
# access_log off;
# }
# 程序内部静态文件配置
# location /statics/ {
# root /opt/xxx/;
# expires 30d;
# access_log off;
# }
# 所有以 /yyy/ 开头的路径的配置
location /yyy/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://127.0.0.1:9000;
}
}
}
保存,退出。
(3):启动Nginx sudo nginx -t -c conf/nginx.conf
(4):补充:平滑重启Nginx的方法
- sudo nginx -s reload
- sudo kill -HUP `主进程ID`
本文详细介绍了如何配置Nginx实现反向代理,包括定位配置文件、修改配置参数、启动及平滑重启Nginx的过程。适用于希望将Nginx作为反向代理服务器的开发者。
936

被折叠的 条评论
为什么被折叠?



