nginx配置本地调试反向代理

2019-12-25  本文已影响0人  halapro_liu

前言

前端在本地调试的时候,调用接口会出现跨域的问题,为了解决这个问题,共有几种方案。

webpack-dev-server进行反向代理

通过webpack-dev-server可以很方便的进行反向代理,今天在这里不再赘述。下面介绍另一种方式,nginx进行反向代理

nginx进行反向代理

image.png

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;


    server {
        listen       8888;
        server_name  localhost;

        location / {
            if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Allow-Origin' '*';
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
                add_header 'Access-Control-Max-Age' 1728000;
                add_header 'Content-Type' 'text/plain; charset=utf-8';
                add_header 'Content-Length' 0;
                return 204;
            }
            if ($request_method = 'POST') {
                add_header 'Access-Control-Allow-Origin' '*';
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
                add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
            }
            if ($request_method = 'GET') {
                add_header 'Access-Control-Allow-Origin' '*';
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
                add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
            }
            proxy_pass http://localhost:8080;
        }

        location /api {
            proxy_pass https://<host>/api;
        }
    }
}

以上配置的作用是监听本地8888端口,并转发请求到8080端口,当请求url带有api时,转发到对应的https://<host>域名。
请求方法为options,post,get时,添加对应的请求头。

上一篇 下一篇

猜你喜欢

热点阅读