Skip to main content

nginx

nginx 操作记录

windows 下载

下载地址

linux 安装

  • Ubuntu 安装 sudo apt install nginx
  • Centos 安装 sudo yum install nginx

Let's Encrypt 安装 https 证书

sudo yum install certbot
sudo yum install python3-certbot-nginx
sudo certbot --nginx -d huangyongcheng.top -d www.huangyongcheng.top -d api.huangyongcheng.top

# 注意,nginx 的 server_name 配置要包含上面的域名,不然无法安装证书
server_name huangyongcheng.top www.huangyongcheng.top api.huangyongcheng.top

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/huangyongcheng.top/fullchain.pem
Key is saved at: /etc/letsencrypt/live/huangyongcheng.top/privkey.pem

# 如果失败了修改 nginx 配置后再执行
certbot install --cert-name huangyongcheng.top

### 证书续期
# Let's Encrypt 的证书有效期为 90 天,需要定期续期,以下是续期命令:
certbot renew

# 自动续期需要设置定时任务,以下是设置定时任务的命令:
sudo crontab -e

# 然后添加以下内容:
0 0 1 * * sudo certbot renew --quiet --renew-hook 'sudo systemctl reload nginx' > /dev/null 2>&1

# 查看定时任务列表:
sudo crontab -l

常用命令

Windows

#### 先 cd 到 nginx 目录下

#查看帮助
nginx -h

#查看 Nginx 的版本号:
nginx -V

#启动 Nginx:
start nginx

#快速停止或关闭 Nginx:
nginx -s stop

#正常停止或关闭 Nginx:
nginx -s quit

#配置文件修改重装载命令:
nginx -s reload

# 查看 windows 任务管理器下 Nginx 的进程命令
tasklist /fi "imagename eq nginx.exe"
# 或者
tasklist | findstr nginx

PS D:\tools\nginx-1.28.2\nginx-1.28.2> tasklist | findstr nginx
nginx.exe 5316 Console 2 8,688 K
nginx.exe 6260 Console 2 9,116 K

# 如果 nginx -s stop 报下面的错,可以尝试 taskkill 命令
PS D:\tools\nginx-1.28.2\nginx-1.28.2> .\nginx.exe -s stop
nginx: [error] invalid PID number "" in "D:\tools\nginx-1.28.2\nginx-1.28.2/logs/nginx.pid"

# 上面已经看到 pid 了,可以直接用 taskkill 命令强制结束进程
taskkill /F /PID 5316
# 根据进程名结束进程
taskkill /F /IM nginx.exe

Linux

# 重启
systemctl restart nginx.service
systemctl restart nginx

踩坑

  • 网站文件不要放在 /root 文件夹下,不然无访问权限,可以放在 /var/www
  • 遇到问题先别慌,到 /var/log/nginx/error.log 中看看错误是什么

nginx.conf

conf/nginx.conf 是对 nginx 的配置

linux 文件位置 : vi /etc/nginx/nginx.conf

#据说配置成跟cup核心数一致就行
worker_processes 4;

http {
#每个server对应一个代理服务,可以代理静态页面,也可以代理位于其他端口的服务
server {
#端口号
listen 8080;

#服务名,取个有意义点的名字,方便自己区分而已,但是不能和其他服务重名
server_name localhost;

#可以在 server 级别配置 root
# root D:/D_Develope/studio;

#字符集,如果不配置应该默认是utf-8吧
#charset koi8-r;

#access_log logs/host.access.log main;

#注意 location 可以配置多个,从上往下匹配,匹配到第一个就会停止匹配

#每个location都是一个路由配置
location / {
#可选,路由对应的本地项目地址,会覆盖 server 配置的 root
root D:/D_Develope/studio;
#默认首页文件
index index.html index.htm dxStudio.html;
}

# 下面的配置 当访问 localhost/jsons 时,会返回 D:/D_Develope/studio/jsons 目录下的文件
location /jsons {
# 注意 studio 下要包含 jsons 文件夹,不然会返回 404 错误
root D:/D_Develope/studio;
}

# root 和 alias 区别:下面的配置 当访问 localhost/jsons/a.json 时,会返回 D:/D_Develope/studio/a.json 文件
location /jsons/ {
# 注意 alias 后面最好带斜杠
alias D:/D_Develope/studio/;
}

location /nextjs {
rewrite ^/nextjs/(.*)$ /$1 break; # 去掉 /nextjs 前缀
proxy_pass http://localhost:3000;
}

# ~ 表示任意路径 ~* 不区分大小写 后面跟正则表达式
location ~* \.(ico|gif|bmp|jpg|jpeg|png|swf|js|css|txt){

}

#指定404错误页面
error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#重定向50开头的错误到指定页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

}
}