手把手教你nginx解决前端本地跨域问题
收到很多私信,有许多同学遇到本地开发静态文件,想要调试远程接口的问题,教大家一个解决跨域调试常用的方法。有什么其他问题也欢迎大家关注私信我,看到的会一一解答。
先下载nginx,因为大部分人是windows 开发环境,所以文章介绍的也是windows版本
打开配置文档,新建一个自己的配置文件my.conf。 然后找到conf/nginx.conf 引入my.conf
配置my.conf文件
server {
listen 3333; #监听端口 可以访问localhost:3333
server_name localhost;
#access_log logs/host.access.log main;
location / {
root C:/Users/86183/Desktop/feedback; # 静态文件路径
index index.html index.htm; # 静态文件首页名字
}
location /api/ { # 请求路劲是api 开头会自动跳转至 www.baidu.com/api/
proxy_pass http://www.baidu.com; #这里填上服务器地址
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
配置成功,在nginx根目录打开 cmd, 命令行输入:
```
nginx版本:nginx -V
启动:start nginx
快速关闭: nginx -s stop
正常关闭:nginx -s quit
重启:nginx -s reload
```
http://localhost:3333/ 查看网站,Ok啦!!!