配置nginx反向代理-解决前后端跨域问题

493 阅读1分钟

1.首先电脑中安装nginx

官方网站下载地址: nginx.org/en/download…

2.解压下载后的文件,目录如下

3.打开conf文件夹下的nginx.conf

找到如下代码注释

#location / {
#    root   html;
#    index  index.html index.htm;
#}

找到如下代码修改

    server {
        listen       8080;
        server_name  localhost;

接着新增

location / {
	proxy_pass http://localhost:8001;
}

location /api/ {
  	proxy_pass http://localhost:8000;
  	proxy_set_header Host $host;
}

其中:

4.点击nginx.exe文件启动nginx

现在访问localhost:8080就不会出现跨域的问题了