1、离线下载地址
https://nginx.org/download/
2、安装
- 解压下载的安装包
tar -zxvf nginx-1.26.0.tar.gz
- 解压之后进入目录执行安装命令
./configure --prefix=/usr/local/nginx --with-stream --with-debug --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module --with-http_realip_module && make && make install
1、 ./configure: 这是在源代码目录中运行的配置脚本。它会检查系统环境并生成适合当前系统的 Makefile 文件,以便后续编译使用。
2、 --prefix=/usr/local/nginx: 这个选项指定了 Nginx 的安装路径。在这个例子中,Nginx 将被安装到 /usr/local/nginx 目录下。
3、 --with-stream: 这个选项启用了 Nginx 的 stream 模块,允许 Nginx 作为一个 TCP/UDP 代理服务器。
3、 --with-debug: 这个选项启用了调试模式,编译的 Nginx 将包含调试信息,有助于诊断问题。
4、 --with-http_stub_status_module: 这个选项启用了 Nginx 的 stub_status 模块,允许通过 HTTP 请求查看 Nginx 的状态信息,比如当前的连接数、请求处理情况等。
5、 --with-http_gzip_static_module: 这个选项启用了 Nginx 的 gzip_static 模块,允许 Nginx 在传送静态文件时进行 gzip 压缩,提高传输效率。
6、 --with-http_flv_module: 这个选项启用了 Nginx 的 flv 模块,允许 Nginx 提供 Flash 视频文件(FLV)的流式传输功能。
7、 --with-http_ssl_module: 这个选项启用了 Nginx 的 SSL 模块,允许 Nginx 提供 HTTPS 服务,即加密的 HTTP 连接。
8、 --with-http_realip_module: 这个选项启用了 Nginx 的 realip 模块,允许 Nginx 识别经过代理服务器传递的真实客户端 IP 地址。
9、 && make && make install: 这是连续执行三个命令的简写形式。make 命令用于编译源代码,make install
命令用于将编译好的程序安装到系统中。这个命令会在配置成功后,编译 Nginx,并将编译后的结果安装到之前指定的安装路径中。
- 启动&停止&退出&重启 命令
cd /usr/local/nginx/sbin/
./nginx #启动
./nginx -s stop #停止 此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
./nginx -s quit #退出 此方式停止步骤是待nginx进程处理任务完毕进行停止。
./nginx -s reload #重启 修改配置后重新加载生效./nginx -s reopen :重新打开日志文件
3、查看nginx进程及杀掉进程
ps -ef|grep nginx #查询进程号
kill -QUIT 主进程号 #从容停止
kill -TERM 主进程号 #快速停止
kill -9 主进程号 #强制停止
4、配置代理
# 负载均衡配置
# weight 设置了服务器的权重,用于负载均衡
# max_fails 定义了在认为服务器不可用之前允许的最大连接失败次数
# fail_timeout 指定了在将服务器标记为暂时不可用之前的失败超时时间
upstream test-upstream{
server 127.0.0.1:8881 weight=1 max_fails=2 fail_timeout=30s;
server 127.0.0.1:8882 weight=2 max_fails=2 fail_timeout=30s;
}
server {
listen 8881;
server_name localhost;
location /test {
root html;
index index.html;
try_files $uri $uri/ /test1/index.html;
}
}
server {
listen 8882;
server_name localhost;
location /test {
# 指定了根目录为html
root html;
index index.html;
try_files $uri $uri/ /test2/index.html;
}
}
server {
listen 8888;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /test3 {
# 指定了别名路径,将请求映射到F:/TemData/test目录。
alias F:/TemData/test;
index index.html;
try_files $uri $uri/ index.html;
}
location /test {
proxy_pass http://test-upstream;
}
}
5、代理数据库
# stream模块,用于配置TCP和UDP代理。
stream {
upstream kingbase {
# 配置数据库的ip和端口
server 127.0.0.1:54321 weight=1 max_fails=2 fail_timeout=30s;
}
server {
# 配置本机暴露端口
listen 6666;
# 设置了与后端服务器建立连接的超时时间为600秒
proxy_connect_timeout 600s;
# 设置了向后端服务器发送请求并等待响应的超时时间为600秒
proxy_timeout 600s;
# 指定了请求转发的目标,即转发到名为oracle的upstream组中的服务器。
proxy_pass kingbase;
}
}
6、请求转发 来源内容
这里我们请求的网站为:192.168.1.0:80/static/a.html
第一种:
location后没有/ 转发网站没有/
location /static{
proxy_pass 192.168.1.1:81
}
最后网址经过nginx转向到的网址是 192.168.1.1:81/static/a.html
第二种:
location后没有/ 转发网站有/
location /static{
proxy_pass 192.168.1.1:81/
}
最后网址经过nginx转向到的网址是 192.168.1.1:81/a.html
第三种:
location后有/ 转发网站没有/
location /static/{
proxy_pass 192.168.1.1:81
}
最后网址经过nginx转向到的网址是 192.168.1.1:81/static/a.html
第四种:
location后有/ 转发网站有/
location /static/{
proxy_pass 192.168.1.1:81/
}
最后网址经过nginx转向到的网址是 192.168.1.1:81/a.html
总结:
从这四种我们可以的看出,当nginx里面匹配时可以把端口后的参数分为path1+path2(其中我在上方标注的location属于path1,proxy_pass属于path2)
当
proxy_pass
里面是ip:port+/时nginx最后匹配的网址是 proxy_pass的内容加上path2
里面是ip:port时nginx最后匹配的网址是 proxy_pass的内容加上path1+path2