Nginx反向代理配置(解决跨域问题)

一. 跨域

   指的是浏览器不能执行其他网站的脚本。它是由浏览器的同源策略造成的,是浏览器对 JavaScript 施加的安全限制。

实际开发过程中表现为,如果本地的Html代码未提交到服务器,本地是不能直接调用服务器 API 获取数据的。

二. Nginx

Nginx 是一个高性能的 HTTP 和反向代理服务器,也是一个IMAP/POP3/SMTP 服务器。其特点是占有内存少,并发能力强,事实上 nginx 的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用 nginx 网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。

三. 反向代理

反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。

四. 利用反向代理解决跨域问题的实际例子

在访问本地http://localhost:9966/lujing/#/basicData/carList页面时,会调用一个接口http://localhost:9966/antColonyTmcsApi/ltsVehicle/listVehicle?_t=1539742798083&pageNo=1&pageSize=10查询车辆列表数据。

因为涉及到跨域问题,所以是调不通的。

解决办法:

 在nginx.conf经过下面的配置后(如最后的配置代码),就可以访问远程服务器上的接口数据了,即解决了跨域问题。

通过下面的两句配置,可以访问 antColonyUmsApi和antColonyTmcsApi远程路径下面的URL

#  antColonyUmsApi 这个路径下面的api
        location /antColonyUmsApi

#  antColonyTmcsApi 这个路径下面的api(http://localhost:9966/antColonyTmcsApi/ltsVehicle/listVehicle?_t=1539742798083&pageNo=1&pageSize=10

location /antColonyTmcsApi 

nginx.conf配置代码如下:

 server {
        listen       9966;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        
        #  antColonyUmsApi 这个路径下面的api
        location /antColonyUmsApi {
        if ($request_method = 'OPTIONS') { 
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
            #
            # Custom headers and headers various browsers *should* be OK with but aren't
            #
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
            #
            # Tell client that this pre-flight info is valid for 20 days
            #
            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-Credentials' 'true'; 
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        }


        if ($request_method = 'GET') {
            add_header 'Access-Control-Allow-Origin' *; 
            add_header 'Access-Control-Allow-Credentials' 'true'; 
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        }
        # 匹配apis之后的路径和参数
        rewrite  ^.+apis/?(.*)$ /$1 break;
        include  uwsgi_params;
        # 实际调用的API
        proxy_pass http://statictest.tflj.com;
       }

       #  antColonyTmcsApi 这个路径下面的api
       location /antColonyTmcsApi {
        if ($request_method = 'OPTIONS') { 
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
            #
            # Custom headers and headers various browsers *should* be OK with but aren't
            #
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
            #
            # Tell client that this pre-flight info is valid for 20 days
            #
            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-Credentials' 'true'; 
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        }


        if ($request_method = 'GET') {
            add_header 'Access-Control-Allow-Origin' *; 
            add_header 'Access-Control-Allow-Credentials' 'true'; 
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        }
        # 匹配apis之后的路径和参数
        rewrite  ^.+apis/?(.*)$ /$1 break;
        include  uwsgi_params;
        # 实际调用的API
        proxy_pass http://statictest.tflj.com;
       }

        #error_page  404              /404.html;

猜你喜欢

转载自blog.csdn.net/robinson_911/article/details/83105795