0%

Nginx

Nginx是一款高性能web服务器

0x0.linux物理机安装

1)命令下载

官网nginx: download找到Stable version下的那个下载

1
wget http://nginx.org/download/nginx-1.24.0.tar.gz

2)解压编译安装

[!CAUTION]

ubuntu 需要预先安装必须库

libssl-dev libpcre3 libpcre3-dev zlib1g-dev make

解压

1
tar -zxvf nginx-1.24.0.tar.gz

编译

cd nginx-1.24.0进入到解压目录执行下面命令,下面列举的是常用编译参数,如果有其他需要可以去查要加哪些模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
./configure  \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin \
--with-http_sub_module \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-pcre \
--without-http_autoindex_module \
--without-http_ssi_module \
--without-http_userid_module \
--with-threads \
--with-http_gzip_static_module \
--with-http_stub_status_module
参数 说明
–prefix=/usr/local/nginx 指定Nginx的安装路径为/usr/local/nginx。你可以根据需要修改安装路径。
–sbin-path=/usr/sbin 指定二进制执行文件目录
–with-http_sub_module 启用子请求替换模块,用于在响应中替换指定的内容。这个模块允许在HTTP响应中使用sub_filter指令来替换文本。
–with-http_ssl_module 启用SSL模块,用于支持HTTPS协议。启用该模块后,Nginx可以作为一个HTTPS服务器来处理加密的HTTP请求。
–with-http_v2_module 启用HTTP/2模块,用于支持HTTP/2协议。启用该模块后,Nginx可以与支持HTTP/2的客户端进行更高效的通信。
–with-http_realip_module 启用真实IP模块,为了告诉被反向代理的服务器,客户端的真实ip地址
–with-pcre 启用PCRE库,用于支持正则表达式。PCRE(Perl Compatible Regular Expressions)是一个正则表达式库,Nginx使用它来进行高效的正则匹配
–without-http_autoindex_module 禁用自动索引模块,该模块允许在没有默认索引文件的情况下列出目录中的文件。禁用该模块后,如果目录没有默认索引文件,Nginx将返回404错误。
–without-http_ssi_module 禁用SSI(Server Side Includes)模块,该模块允许在HTML文件中嵌入动态内容。禁用该模块后,Nginx将不再处理SSI指令。
–without-http_userid_module 禁用用户ID模块,该模块用于提取和设置用户ID和组ID。禁用该模块后,Nginx将不再处理与用户ID相关的功能。
–with-threads 启用线程支持,使Nginx能够利用线程处理请求,从而提高性能和响应能力。
–with-http_gzip_static_module 启用gzip静态压缩模块,用于在服务器上压缩静态文件。启用该模块后,Nginx可以自动压缩支持gzip的文件,并在客户端请求时进行解压缩。
–with-http_stub_status_module 启用状态监控模块,可以通过访问/nginx_status路径获取Nginx的运行状态。该模块提供了Nginx服务器的一些统计信息,如活跃连接数、请求处理数量等。

安装

1
make && make install

这样安装完,配置文件在/usr/local/nginx/conf/nginx.conf

0x1.配置

告诉后端服务器,客户端的真实ip

编译的时候加了ngx_http_realip_module这个模块就是为了这个,具体配置如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
http {
# 其他配置...

# 定义一个适当的 IP 地址或 IP 段作为真实 IP 的来源,如果请求直接来自客户端,可不配置
#set_real_ip_from 10.0.0.0/8;
#set_real_ip_from 192.168.0.0/16;

# 定义需要重写的 HTTP 头字段,用于存储真实 IP
real_ip_header X-Forwarded-For;

# 启用 ngx_http_realip_module 模块
real_ip_recursive on;

# 其他配置...
}

格式化输出日志

1
2
3
4
5
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
}

日志输出内容类似这样

1
162.158.178.89 - - [05/Jul/2023:02:22:33 -0400] "GET /hello HTTP/1.1" 200 11 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.67"

开启gzip压缩

1
2
3
4
5
6
7
8
9
10

http {
gzip on; #表示开启 gzip 压缩功能
gzip_comp_level 5; #压缩级别,范围从 1 到 9,9 表示最高压缩比但消耗更多的 CPU 资源。
gzip_min_length 256; #启用压缩的最小文件大小,文件大小低于该值将不会被压缩。
gzip_proxied any; # 指定哪些响应可以被压缩,any 表示所有响应都可以被压缩。
gzip_vary on; # 在响应头中添加 Vary: Accept-Encoding,用于通知客户端已经进行了压缩。
# 指定要压缩的 MIME 类型
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
}

禁止使用IP地址访问

1
2
3
4
5
6
 server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 100 ;
}

404自定义返回

1
2
3
4
5
6
7
8
9
10
server {
listen 80;
server_name yourdomain.com;
error_page 404 = @redirect;
location @redirect {
# return 302 http://www.example.com/404-page; # 重定向
add_header Content-Type text/plain;
return 200 "Nothing here";
}
}

配置返回文本

1
2
3
4
location /hello {
add_header Content-Type text/plain;
return 200 "hello world";
}

配置反向代理v2ray

1
2
3
4
5
6
7
8
9
10
location /v2ray {
proxy_redirect off;
proxy_pass http://127.0.0.1:10000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

配置反向代理cockpit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server {
listen 80;
server_name www.example.com;

location / {
proxy_pass https://localhost:9090; # 将流量转发到服务器的端口X
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;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Accept-Encoding "";
}
}
赏口饭吃吧!