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服务器的一些统计信息,如活跃连接数、请求处理数量等。 |
安装
这样安装完,配置文件在/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 {
real_ip_header X-Forwarded-For;
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_comp_level 5; gzip_min_length 256; gzip_proxied any; gzip_vary on; 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 { 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; 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 ""; } }
|