
server {
    listen 80 default_server;
    server_name _;


    # 启动Gzip Json模式 ######################################################
	gzip_http_version 1.0;  # gzip支持http协议 proxy 必须用
    gzip  on;
	gzip_vary on;
	gzip_proxied       any;
	gzip_static on;
	gzip_comp_level  4;
	gzip_min_length 256;
    gzip_buffers     4 8k;
    gzip_types       text/html text/plain application/javascript application/x-javascript text/css application/xml application/json;

    client_header_timeout 300;
    client_body_timeout 300;
    keepalive_timeout 300;
    proxy_connect_timeout 300;
    proxy_send_timeout 300;
    proxy_read_timeout 300;

    location /api {
        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' '*' always;
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
            add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Access-Control-Allow-Origin' always;
            add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
        }
        if ($request_method = 'GET') {
            add_header 'Access-Control-Allow-Origin' '*' always;
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
            add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Access-Control-Allow-Origin' always;
            add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
        }

        rewrite ^/(.*)$ /$1 break;
        proxy_pass http://127.0.0.1:61336/api;
    }

    location /parse/{
	proxy_read_timeout 300;
        proxy_connect_timeout 300;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://127.0.0.1:61336/parse/;
        proxy_redirect off;
    }

    location /docs/ {
        proxy_read_timeout 300;
        proxy_connect_timeout 300;
    	client_body_buffer_size 500m;
	client_max_body_size 1000m;

        rewrite ^/docs/(.*)$ /$1 break;  # 去掉/docs/前缀

        proxy_pass http://localhost:3000;  # 反向代理到本地的3000
        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_set_header X-Forwarded-Proto $scheme;
    }

    root /var/www/edu-textbook/;
    location /{
        try_files $uri $uri/ /index.html?$query_string;
    }

}