Nginx

安装 Nginx

1dnf install -y nginx
2systemctl start nginx # 启动 Nginx
3systemctl enable nginx # 设置开机自启

Nginx 配置文件

nginx.conf

nginx.conf 是 Nginx 的主配置文件,通常位于 /etc/nginx/nginx.conf,包含了全局配置和一些默认的规则。

/conf.d

/conf.d 是 Nginx 的配置文件目录,通常位于 /etc/nginx/conf.d,用于存放特定的配置文件。在该目录下可以创建多个以 .conf 为后缀的配置文件,这样可以将配置逻辑分开,使得配置更加模块化、易于管理。

为 Nest API 设置 Nginx

1vi /etc/nginx/conf.d/nest.conf # 编辑 Nginx 配置文件
1server {
2 server_name example.com;
3  charset 'utf-8';
4
5  location / {
6    proxy_pass http://localhost:3000;
7    proxy_http_version 1.1;
8    proxy_set_header Upgrade $http_upgrade;
9    proxy_set_header Connection 'Upgrade';
10    proxy_set_header Host $host;
11    proxy_cache_bypass $http_upgrade;
12    proxy_set_header X-Real-IP $remote_addr;
13    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
14    proxy_set_header Access-Control-Allow-Origin https://xxx,http://localhost:4060; # 跨域配置
15  }
16
17  location = /favicon.ico {
18    access_log off;
19    log_not_found off;
20  }
21  location = /robots.txt {
22    access_log off;
23    log_not_found off;
24  }
25}
1nginx -t # 检查配置文件是否正确
2systemctl restart nginx # 重启 Nginx

常见问题

(13: Permission denied) while connecting to upstream:[nginx]