使用nginx搭建linux的http代理

想在linux上使用http代理,方便拉代码和装软件依赖。
                              —— By Jihan


首先你需要准备一个能够访问外网的服务器,作为流量跳板使用。如果没有,可以去买一个。
搭建代理就两步:

  1. 在服务器上安装配置nginx
  2. 配置linux客户端上的proxy代理。

服务端

别人写的挺好的,我就不抄了。如果你想实际从源码编译,可以参考下面的链接:
配置nginx正向代理
nginx 正向代理https配置

我这里提供一下我编译好的nginx包:
centos7.5: nginx_centos
Ubuntu 7.3: nginx_ubuntu

对应的配置文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
user www-data;
worker_processes auto;
pid /run/nginx.pid;
#include /etc/nginx/modules-enabled/*.conf;

events {
worker_connections 768;
# multi_accept on;
}

http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;

# server_names_hash_bucket_size 64;
# server_name_in_redirect off;

include /etc/nginx/mime.types;
default_type application/octet-stream;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

gzip on;

#set http and https proxy
server {
resolver 8.8.8.8;
resolver_timeout 5s;
listen 7080;

proxy_connect;
proxy_connect_allow all;
proxy_connect_connect_timeout 10s;
proxy_connect_read_timeout 10s;
proxy_connect_send_timeout 10s;

location / {
proxy_pass $scheme://$host$request_uri;
proxy_set_header Host $http_host;
proxy_buffers 256 4k;
proxy_max_temp_file_size 0;
proxy_connect_timeout 30;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 301 1h;
proxy_cache_valid any 1m;
}
}
}

客户端

非常简单,设置环境变量就行:

1
2
export http_proxy=http://xxx.xxx.xxx.xxx:7080
export https_proxy=http://xxx.xxx.xxx.xxx:7080

观点2

观点3

其他

参考:

-------------本文结束感谢您的阅读-------------