• Nginx 设置cors跨域


    在我们的开发中,经常遇到跨域,这个时候,可以通过cors来解决。

    解决的方法可以在服务端的代码层或者在web服务器进行设置

    在web服务器上进行设置cors 跨域,这样就不必改动代码。以nginx为例子

    提示:有时候我们的后端是PHP文件,则需要把跨域的代码加 location ~ .php(.*)$ 中。

    location / {
         if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            #
            # Custom headers and headers various browsers *should* be OK with but aren't
            #
            add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,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-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';
         }
    }
    

      另外一种可以设置反向代理

    location ^~ /api/v1 {
    	add_header 'Access-Control-Allow-Origin' "$http_origin"; 
    	add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS'; 
    	add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type    '; 
    	add_header 'Access-Control-Allow-Credentials' 'true'; 
    	if ($request_method = 'OPTIONS') { 
    		add_header 'Access-Control-Allow-Origin' "$http_origin"; 
    		add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS'; 
    		add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type    '; 
    		add_header 'Access-Control-Allow-Credentials' 'true'; 
    		add_header 'Access-Control-Max-Age' 1728000; # 20 天 
    		add_header 'Content-Type' 'text/html charset=UTF-8'; 
    		add_header 'Content-Length' 0; 
    		return 200; 
    	} 
        # 这下面是要被代理的后端服务器,它们就不需要修改代码来支持跨域了
    	proxy_pass http://127.0.0.1:8085; 
    	proxy_set_header Host $host; 
    	proxy_redirect off; 
    	proxy_set_header X-Real-IP $remote_addr; 
    	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    	proxy_connect_timeout 60; 
    	proxy_read_timeout 60; 
    	proxy_send_timeout 60; 
    
    }
    

      

  • 相关阅读:
    Sonar+IDEA + Maven的集成
    My97DatePicker IE9中,显示全部为1
    Datatable+jeditable+Java 结合使用实现表格的单行刷新
    Datatables表格控件的使用相关网站及遇到的问题
    xss 小练习
    主页面布局 随浏览器大小变化而变化
    使用angular中自定义的directive实现删除确认框
    使用css和js完成模态弹窗功能
    ckplayer插件的使用
    使用JQuery进行表格分页查询
  • 原文地址:https://www.cnblogs.com/tl542475736/p/9465681.html
Copyright © 2020-2023  润新知