跳至正文

含配置:nginx反代chunkbase

由于家里的网络访问chunkbase是真的不行

于是,我就有了使用nginx作为反向代理的想法

经过了一顿折腾,以下是我想出来的最优配置文件

缓存版(最稳定):

worker_processes  1;

error_log  /dev/stderr;  # 重定向输入输出

pid nginx.pid;

daemon off;  # 关闭守护进程,让nginx能前台运行

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    access_log  /dev/stdout;

    sendfile        on;

    keepalive_timeout  65;

    #gzip  on;
	
    proxy_temp_path proxy_temp;  # 设置会话临时文件夹
    proxy_cache_path proxy_cache levels=1:2 keys_zone=cache:200m;  # 设置缓存
    #proxy_cache_path proxy_cache levels=1:2 keys_zone=cache:200m inactive=3d max_size=2g;  # 缓存备选方案
    
    server {
        listen       8080;  # 监听端口号
        server_name  localhost;

        #charset koi8-r;
        
        proxy_buffering on;
	
        location / {
			proxy_pass https://www.chunkbase.com;  # 要反代的源站
			expires 7d;  # 设置过期时间
		
			proxy_cache cache;
		
			#proxy_set_header User-Agent "NginxProxy/1.20.2";  # 设置UA,如果网站限制UA的话可以修改为浏览器的UA
        	proxy_set_header Host www.chunkbase.com;  # 设置代理主机为chunkbase,因为chunkbase套了一层cdn,需要域名识别
        	proxy_set_header X-Real-IP $remote_addr;
        	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        	
        	add_header Access-Control-Allow-Origin *;  # 允许跨域
        	#add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";  # 可选
        }
      }
    }

	# 如果你有HTTPS代理的需求,你可以在上一个server块里重定向到https
    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

永久缓存版(不推荐):

该版本基本一样,不过location字段需要修改

        location / {
             	if (!-e $request_filename) {
			proxy_pass https://www.chunkbase.com;
		}
             	root store_data;
             	proxy_store on;
             	proxy_set_header Host www.chunkbase.com;
        	proxy_set_header X-Real-IP $remote_addr;
        	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_store_access user:rw group:rw all:r;
		
        	autoindex on;
        	autoindex_localtime on;
        	expires 7d;
        }
      }

同理,如果 想要反代其他网站,一定要先检查网站限制!

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注