1. 程式人生 > 實用技巧 >nginx跨域配置

nginx跨域配置

主要是以下幾行內容

        add_header Access-Control-Allow-Origin *;
        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,Authorization';

        if ( $request_method = 'OPTIONS') {
             return 204;
        }

加在server裡面

    server {
        listen 80 ;

        server_name  localhost;
        access_log  logs/access.log  main;

        add_header Access-Control-Allow-Origin *;
        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,Authorization
'; if ( $request_method = 'OPTIONS') { return 204; } location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http:
//1873; proxy_buffer_size 128k; proxy_buffers 32 32k; proxy_busy_buffers_size 128k; proxy_connect_timeout 60; proxy_send_timeout 60; proxy_read_timeout 60; send_timeout 60; } }