文章目录
- 基本配置
- 报错解决
- 只能通过`[域名]:[端口]/[API路径]`的方式请求
基本配置
user www-data;
worker_processes auto;error_log /var/log/nginx/error.log notice;
pid /run/nginx.pid;events {worker_connections 1024;
}http {include /etc/nginx/mime.types;default_type application/octet-stream;client_max_body_size 15m; log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';server {listen 80;server_name [域名] www.[域名];# root /var/www/dist;# index index.html index.htm;# 对所有请求返回403 Forbiddenlocation / {return 403 "网站暂时不可用";}}server {listen 443 ssl;server_name [域名] www.[域名];ssl_certificate /root/xxx.pem;ssl_certificate_key /root/xxx.key;ssl_session_timeout 5m;ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;ssl_protocols TLSv1 TLSv1.1 TLSv1.2;ssl_prefer_server_ciphers on;# 对所有请求返回503 Service Unavailablelocation / {return 503 "网站正在维护中";}location /admin { ## 前端项目alias /usr/share/nginx/html/admin/;autoindex on;try_files $uri $uri/ /admin/index.html;index index.html index.htm;break;}location /app-api/ { ## 后端项目 - Appproxy_pass http://xxxxx:30000/app-api/;proxy_set_header Host $host;client_max_body_size 20m;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "upgrade";}location /admin-api/ { ## 后端项目 - Adminproxy_pass http://xxxxx:30000/admin-api/;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_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "upgrade";}location /device-api/ {proxy_pass https://xxxxx:30001/device-api/;proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "upgrade";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 X-Forwarded-Proto $scheme;}location /default_avatar {alias /var/www/static/default_avatar.png;add_header Content-Type image/png;}location /default_ip_figure {alias /var/www/static/default_ip_figure.png;add_header Content-Type image/png;}}server {listen 30002;location /tts-api/ {proxy_pass http://xxxxx:30002/tts-api/;proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "upgrade";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 X-Forwarded-Proto $scheme;}}server {listen 8002;location / {proxy_pass http://xxxxx:8002;proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "upgrade";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 X-Forwarded-Proto $scheme;}}server {listen 7860;location / {proxy_pass http://xxxx:7860;proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "upgrade";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 X-Forwarded-Proto $scheme;proxy_max_temp_file_size 0;client_max_body_size 10m;client_body_buffer_size 128k;proxy_connect_timeout 90;proxy_send_timeout 90;proxy_read_timeout 90;proxy_buffer_size 4k;proxy_buffers 4 32k;proxy_busy_buffers_size 64k;proxy_temp_file_write_size 64k;}}access_log /var/log/nginx/access.log main;sendfile on;#tcp_nopush on;keepalive_timeout 65;#gzip on;include /etc/nginx/conf.d/*.conf;
}
报错解决
只能通过[域名]:[端口]/[API路径]
的方式请求
由于网管局规定,我的主站域名443端口的/需要为关闭状态,而当修改完之后,所有的请求都只能通过+端口号的方式请求。
- 报错原因
我的请求是https的,而在conf中配置的端口映射pass为proxy_pass http://xxxxxxx:30001/device-api/;
,因此导致了无法请求,当我直接使用域名时,相当于不经过路由直接请求https,则为正常,因此只需将其修改为https的版本proxy_pass https://xxxxxxx:30001/device-api/;
即可。