简介

本文记录了 Nginx 的常用安装、卸载、配置、命令及代理配置等内容,便于日常查阅。


安装


卸载

  • Windows:直接删除安装目录
  • Linux
    1. 查看 Nginx 是否运行:ps -ef | grep nginx
    2. 停止服务:/usr/sbin/nginx -s stopnginx -s stop
    3. 检查端口:netstat -lntp,确认已停止
    4. 查找相关文件:whereis nginx,并删除(如:rm -rf /usr/sbin/nginx
    5. 使用包管理器卸载:yum remove nginx

配置

  • IP 的 SSL 配置
  • 开机自启动
    1. 查找安装路径:whereis nginx(如 /usr/local/nginx/sbin/nginx/usr/bin/nginx
    2. 编辑 /etc/rc.local,末尾添加 Nginx 启动命令
    3. 修改权限:chmod 755 /etc/rc.local
    4. 重启测试:shutdown -r now

常用命令

  • 安装:brew install nginx
  • 查看信息:brew info nginx
  • 启动:nginx
  • 重新加载配置:nginx -s reload
  • 重启:nginx -s reopen
  • 停止:nginx -s stop(强制杀进程)
  • 优雅退出:nginx -s quit(处理完请求后退出)
  • 查看版本及配置路径:nginx -V
  • 查看版本:nginx -v
  • 指定配置文件:nginx -c filename
  • 帮助:nginx -h
  • 修改配置文件:vi /usr/local/etc/nginx/nginx.conf
  • 检查配置语法:nginx -t

代理配置(支持 HTTPS、WSS)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#PROXY-START/
location ~* \.(php|jsp|cgi|asp|aspx)$
{
proxy_pass http://182.61.6.105:9003;
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_set_header REMOTE-HOST $remote_addr;
}
location /
{
proxy_pass http://182.61.6.105:9003;
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_set_header REMOTE-HOST $remote_addr;

add_header X-Cache $upstream_cache_status;

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

# 禁用缓存
add_header Cache-Control no-cache;
expires 12h;
}
#PROXY-END/

常见问题

  • 配置文件修改后,务必执行 nginx -t 检查语法,再重载服务。
  • 端口占用、权限不足等问题,可通过日志和命令行排查。