nginx跨域问题 CORS policy: No 'Access-Control-Allow-Origin'

wylc123 1年前 ⋅ 2314 阅读

1. 问题描述

公司网站添加ssl配置,从http访问转为https访问,在调用onlyoffice服务器显示报告时,

前端浏览器报错:

XXXXX(请求的跨域url)has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested

2. 问题解决

nginx解决跨域问题需要在http下添加跨域配置:

add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;

例如:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
	add_header Access-Control-Allow-Origin *;
	add_header Access-Control-Allow-Headers X-Requested-With;
	add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
	
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
	client_max_body_size 100m;
	upstream docservice {
        server 10.80.64.194:9000;
    }


	map $http_x_forwarded_proto $the_scheme {
		default $http_x_forwarded_proto;
		"" $scheme;
	}

	map $http_x_forwarded_host $the_host {
		default $http_x_forwarded_host;
		"" $host;
	}

	map $http_upgrade $proxy_connection {
		default upgrade;
		"" close;
	}

	proxy_set_header Upgrade $http_upgrade;
	proxy_set_header Connection $proxy_connection;
	proxy_set_header X-Forwarded-Proto $the_scheme;
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;


    server {
        listen       80;
        server_name  localhost;

        location /onlyoffice/ {
            proxy_pass http://docservice/;
            proxy_http_version 1.1;
            proxy_set_header X-Forwarded-Host $the_host:$server_port/onlyoffice;
        }

        location / {
            proxy_pass   http://127.0.0.1:8080/;
            proxy_http_version 1.1;
            proxy_set_header X-Forwarded-Host $the_host:$server_port;
            proxy_set_header Host $host:$server_port;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

}

 

更多内容请访问:IT源点

相关文章推荐

全部评论: 0

    我有话说: