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

常用命令

Windows

查看帮助 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"

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 / {
#可选,路由对应的本地项目地址,会覆盖 server 配置的 root
root D:/D_Develope/studio;
#默认首页文件
index index.html index.htm dxStudio.html;
}

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;
}

}
}