前端打包后在自己电脑上配置nginx 解决跨域问题

下载Nginx http://nginx.org/en/index.html

下载以后得Nginx最好存放路径不要有中文名称

 配置Nginx   

默认端口号80(如果打开闪一下就退出可能是端口号占用)

这时打开nginx包中conf包里的nginx.conf文件  大概在36行

配置跨域

也在conf包中的nginx.conf文件中 刚刚配置好的端口号下方配置跨域信息

#跨域
location /device/ {  
        proxy_pass http://127.0.0.1:8092/device/; //你的后端端口 
        proxy_http_version 1.1;  
        proxy_set_header Upgrade $http_upgrade;  
        proxy_set_header Connection 'upgrade';  
        proxy_set_header Host $host;  
        proxy_cache_bypass $http_upgrade;  
  
        # 允许跨域  
        add_header 'Access-Control-Allow-Origin' 'http://127.0.0.1:8550' always;  //你刚刚配置的nginx端口
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;  
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization' always;  
   add_header 'Access-Control-Allow-Credentials' true;
        # 如果需要处理 OPTIONS 预检请求  
        if ($request_method = 'OPTIONS') {  
            add_header 'Access-Control-Max-Age' 1728000;  
            add_header 'Content-Type' 'text/plain charset=UTF-8';  
            add_header 'Content-Length' 0;  
            return 204;  
        }  
    }  

这时就可以启动Nginx  双击nginx.exe  允许访问防火墙

配置完成就可以访问了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值