express + nginx 项目配置问题

查看 78|回复 2
作者:qingshui33   
express + nginx 项目,代码上线后,单独访问接口是可以的,但是在前端项目中调接口的时候就报 405 ,有大哥知道这是怎么回事吗? nginx 配置如下:
server {
        listen 80;
    listen 443 ssl;
    server_name xxx;
    ssl_certificate /usr/local/nginx/conf/ssl/xxx.com.pem;
    ssl_certificate_key /usr/local/nginx/conf/ssl/xxx.com.key;
    ssl_protocols TLSv1.2;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-NginX-Proxy true;
                   proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header X-Forwarded-Proto $scheme;
                proxy_set_header X-Forwarded-Method $request_method;
                add_header 'Access-Control-Allow-Origin' '*';
                  add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
                  add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
                add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
    }
}
前端项目配置了跨域,代码如下:
import { defineConfig } from "vite";
export default defineConfig({
  ...
  server: {
    open: true,
    proxy: {
      "/api": {
        target: nginx 中配置的 server_name ,
        changeOrigin: true,
      },
    },
  },
...
});
请各位大哥指点一下

Nginx, add_header, proxy_set_header, SSL

longgediyi999   
Nginx
location /api {
proxy_pass http://127.0.0.1:3000;
}
重启项目和 Nginx 试试
qingshui33
OP
  
@longgediyi999 刚刚试了下,不行
您需要登录后才可以回帖 登录 | 立即注册

返回顶部