背景:生产环境中使用nginx转发,前后端分别采用一个server部署,前后端接口数据能交互,前端无法下载存在后端的静态文件,本文采用一个server的方法,后端以一个location的方式代理后端服务,解决了跨域问题。
注:跨域的解决方法有多种,本文仅提供一种解决方法,有更好的办法你可以指教一下我,谢谢!
直接上nginx配置文件
user root;
worker_processes 1;
#error_log /var/logs/error.log;
#error_log /var/logs/error.log notice;
#error_log /var/logs/error.log info;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# server配置
server {
# listen 8080;
listen 8080;
server_name developer_server;
charset utf-8;
location / {
try_files $uri $uri/ @router;#需要指向下面的@router否则会出现vue的路由在nginx中刷新出现404
root /root/developer_ui/dist/; # 前端build后的目录地址,绝对路径
add_header Cache-Control max-age=no-cache;