软件环境
Nginx 官方网站:nginx
Nginx 项目源码:https://github.com/nginx/nginx
Nginx 帮助文档:nginx documentation
Nginx 文档:NGINX Documentation
一、背景
网上写的好的贴子的确很多,不过读者觉得太啰嗦,自己记一篇,至少更适合自己
二、使用场景
1、所有内网应用都不能直接访问外网,但需要能通过(内部的)外网代理服务器访问
2、自己不能上网,蹭别人的网,局域网代理软件Ccproxy也是一种正向代理
3、大家访问外国网站使用的梯子(翻墙)也是一种正向代理
三、环境
- Nginx 1.27.13 官方下载地址:Nginx 1.27.3
- ngx_http_proxy_connect_module 1.27.1 官方下载地址:ngx_http_proxy_connect_module
四、下载
1、nginx源代码安装包下载见上面的环境单节
2、【特别注意】proxy_connect模块下载
五、安装
1、在线安装
# 安装 EPEL 存储库
sudo yum install epel-release -y
# 安装 nginx
sudo yum install nginx -y
2、离线安装
-》依赖(存在则跳过)
# 编译nginx前或者在编译过程中需要安装一些依赖包
yum install gcc
yum install gcc-c++
yum install zlib openssl pcre
-》重点
# 打补丁
patch -p1 < ../../modules/ngx_http_proxy_connect_module-0.0.5/patch/proxy_connect_rewrite_102101.patch
# 如果安装了prce 最后一个参数可以不加
./configure --prefix=/app/nginx --sbin-path=/app/nginx --add-module=/app/nginx/modules/ngx_http_proxy_connect_module-0.0.5 --without-http_rewrite_module
# 重新编译安装
make -j 2
make install
六、Nginx参考配置
#user nobody;
worker_processes auto;
worker_cpu_affinity auto;
worker_rlimit_nofile 65536;
error_log logs/error.log error;
#pid logs/nginx.pid;
events {
worker_connections 16384;
}
http {
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;
client_max_body_size 30M;
#keepalive_timeout 0;
server_tokens off;
limit_conn perip 60000;
keepalive_timeout 65;
client_body_timeout 60;
client_header_timeout 60;
send_timeout 60;
limit_rate 10m;
limit_conn_zone $binary_remote_addr zone=perip:50m;
#gzip on;
server {
resolver 114.114.114.114;
listen 9999;
resolver_timeout 5s;
proxy_connect;
proxy_connect_allow 443 563;
proxy_connect_connect_timeout 30s;
proxy_connect_read_timeout 30s;
proxy_connect_send_timeout 30s;
access_log logs/access_9999.log main;
error_log logs/error_9999.log info;
error_page 413 /413.html;
error_page 400 /400.html;
error_page 404 /404.html;
error_page 502 /502.html;
error_page 504 /504.html;
server_tokens off;
limit_rate 10m;
limit_conn perip 60000;
keepalive_timeout 65;
client_body_timeout 60s;
client_header_timeout 60s;
send_timeout 60s;
location / {
proxy_pass http://$http_host$request_uri;
proxy_set_header HOST $http_host;
proxy_buffers 256 4k;
proxy_max_temp_file_size 0k;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_next_upstream error timeout invalid_header http_502;
limit_rate 10m;
limit_conn perip 60000;
}
}
}
七、总结
作者重点写正向代理使用,但看到网上正向代理的贴子实在太啰嗦,故把安装也写了一下,安装的重点是ngx_http_proxy_connect_module,按官方指导下对应版本的补丁,打完补丁重新编译安装nginx即可。
附件一:CentOS上nginx基本操作
在CentOS上安装和配置Nginx是一个常见的任务,特别是在需要高性能的Web服务器环境时。以下是一些基本的Nginx操作,包括安装、启动、停止、重启和配置。
1. 安装Nginx
在CentOS上安装Nginx,你可以使用yum
包管理器。首先,确保你的系统的包列表是最新的:
sudo yum update
然后,安装Nginx:、
sudo yum install epel-release
sudo yum install nginx
2. 启动Nginx
安装完成后,你可以使用以下命令来启动Nginx:
sudo systemctl start nginx
3. 停止Nginx
如果你需要停止Nginx,可以使用:
sudo systemctl stop nginx
4. 重启Nginx
当修改了配置文件并希望应用这些更改时,你需要重启Nginx:
sudo systemctl restart nginx
5. 检查Nginx状态
要检查Nginx服务的状态,可以使用:
sudo systemctl status nginx
6. 设置Nginx开机自启
确保Nginx在系统启动时自动启动:
sudo systemctl enable nginx
7. 配置Nginx
Nginx的配置文件通常位于/etc/nginx/nginx.conf
。你可以根据需要编辑这个文件来配置虚拟主机、代理设置等。例如,创建一个新的虚拟主机配置:
-
创建新的配置文件,例如
/etc/nginx/conf.d/mydomain.conf
:
sudo vi /etc/nginx/conf.d/mydomain.conf
在文件中添加如下内容:
server {
listen 80;
server_name mydomain.com www.mydomain.com;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
-
测试配置文件是否有语法错误:
sudo nginx -t
-
如果测试通过,重启Nginx使配置生效:
sudo systemctl restart nginx
8. 查看日志文件
Nginx的日志文件通常位于/var/log/nginx/
目录下。你可以查看access.log
和error.log
来了解服务器的访问和错误信息。例如:
sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log
通过以上步骤,你可以在CentOS系统上成功安装、配置和操作Nginx。