现在用python的fastapi做了一个后台部署在服务器上,前端采用的vue-ant-admin,直接调用fastapi一直出错
发现是跨域问题,按照网上查询的fastapi做跨域配置:
import uvicorn from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware app = FastAPI() origins = [ "http://localhost.tiangolo.com", "https://localhost.tiangolo.com", "http://localhost", "http://localhost:8080", ] app.add_middleware( CORSMiddleware, allow_origins=origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) @app.get("/") async def main(): return {"message": "Hello World"} if __name__ == '__main__': uvicorn.run(app='main:app', host="0.0.0.0", port=8000, reload=True, debug=True)
这个方法没用,还是有跨域问题!!!!
没办法 ,只能通过nginx转发接口,把允许跨域的配置配在了nginx配置中
server { listen 6013; location / { proxy_pass http://127.0.0.1:6012/; add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT, X-CustomHeader, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type'; } }
如果遇到这个问题:
[emerg] bind() to 0.0.0.0:8380 failed (13: Permission denied)
首先,查看http允许访问的端口:
semanage port -l | grep http_port_t
http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000
http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000
其次,将要启动的端口加入到如上端口列表中
emanage port -a -t http_port_t -p tcp 8090
文章评论