安装

  • win:下载解压安装
  • linux
    • CentOS安装文档—>nginx
    • 找文件姿势find / -name nginx
    • 配置参考官方文档或config
    • 也可以在docker安装,或者通过bt.cn安装

卸载

  • win:直接删掉即可
  • linux
    • ps -ef |grep nginx查看nginx是否在运行
    • /usr/sbin/nginx -s stop or nginx -s stop如果运行,需要停止服务
    • netstat -lntp查看nginx如果不在说明已经停止掉
    • whereis nginx查看nginx相关文件并删除,如:rm -rf /usr/sbin/nginx or rm -f /usr/sbin/nginx.tar.gz
    • yum remove nginx使用yum清理nginx

配置

  • ip的ssl配置
  • 开机自启动
    • whereis nginx查看安装路径(如:/usr/local/nginx/sbin/nginx 或者 /usr/bin/nginx
    • 编辑文件vi /etc/rc.local,在最后一行添加上安装路径即可
    • 更改文件权限:cd /etc & chmod 755 rc.local
    • 重启系统试试:shutdown -r now

常用命令

  • brew install nginx# 安装
  • brew info nginx查看信息和配置文件所在路径
  • nginx启动
  • nginx -s reload重新加载配置
  • nginx -s reopen重启
  • nginx -s stop停止(此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。)
  • nginx -s quit退出(此方式停止步骤是待nginx进程处理任务完毕进行停止。)
  • nginx -V查看版本,以及配置文件地址
  • nginx -v查看版本
  • nginx -c filename指定配置文件
  • nginx -h帮助
  • vi /usr/local/etc/nginx/nginx.conf修改配置文件
  • nginx -t# 查看修改后的配置是否正确

nginx代理配置,支持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
29
#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";

#Set Nginx Cache

add_header Cache-Control no-cache;
expires 12h;
}
#PROXY-END/

小技巧

  • nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)端口占用报错
  • nginx: [alert] could not open error log file: open() “/usr/local/var/log/nginx/error.log” failed (13: Permission denied)权限不够?sudo+命令