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,
},
},
},
...
});
请各位大哥指点一下
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.