文章目录
Nginx的发音是 “engine-x”
Nginx (pronounced “engine-x”) is an open source reverse proxy server for HTTP, HTTPS, SMTP, POP3, and IMAP protocols, as well as a load balancer, HTTP cache, and a web server (origin server). The nginx project started with a strong focus on high concurrency, high performance and low memory usage. It is licensed under the 2-clause BSD-like license and it runs on Linux, BSD variants, Mac OS X, Solaris, AIX, HP-UX, as well as on other *nix flavors. It also has a proof of concept port for Microsoft Windows.
什么是Nginx
Nginx是速度最快的Web服务器
俄罗斯人开发
完全使用C语言开发
特点:
- 并发能力强,最高可支持 50000 个并发连接
- 高性能,内存小
- 可在不间断服务的情况下进行软件版本的升级
正向代理与反向代理
正向代理
- 代理客户端:正向代理代表客户端发起请求。客户端通过配置浏览器或应用程序使用正向代理,将所有的请求发送到代理服务器
- 隐藏真实客户端:正向代理的一个重要作用是隐藏真实客户端的身份。目标服务器只能看到代理服务器发起的请求,而不知道最终请求的发起者是哪个客户端
- 访问控制:正向代理可以用于实施访问控制策略,例如限制特定客户端访问某些资源,或者通过认证机制对客户端进行身份验证
- 突破访问限制:正向代理可以帮助客户端绕过一些访问限制,例如访问被封锁的网站。因为目标服务器只看到代理服务器的请求,而不知道实际客户端的IP地址
- 内容过滤和日志记录:正向代理可以用于过滤或记录客户端请求和响应的内容。这对于监控和审计网络流量非常有用
- 缓存:正向代理可以缓存已经访问过的资源,以提高性能并减少对目标服务器的请求次数。这有助于减轻目标服务器的负载
反向代理
- 代理服务器在服务器端:反向代理的代理服务器位于目标服务器的后端,代表服务器对外提供服务。客户端只与反向代理服务器通信,而不直接与目标服务器交互
- 隐藏真实服务器:反向代理隐藏了真实服务器的身份。客户端无法直接访问目标服务器,因为它只能与反向代理服务器通信。这有助于提高服务器的安全性和隐私性
- 负载均衡:反向代理可以用于实现负载均衡,将客户端请求分发到多个后端服务器上。这有助于提高系统的性能和可伸缩性
- SSL加密和解密:反向代理可以负责处理客户端和服务器之间的SSL加密和解密,减轻了服务器的负担。客户端与反向代理之间使用SSL加密通信,而反向代理与后端服务器之间使用常规的HTTP通信
- 缓存:反向代理可以缓存目标服务器的响应,以提高性能并减轻后端服务器的负载。这对于减少重复请求和加速内容传输非常有用
- 安全性增强:反向代理可以用于增强安全性,例如过滤恶意请求、防止DDoS攻击等
正向代理是代理客户端,反向代理是代理服务端,所以相对于代理客户端叫反向代理
(正向代理隐藏客户端请求,反向代理隐藏服务端请求)
负载均衡与动静分离
负载均衡
- 轮询(Round Robin):默认的负载均衡算法,依次将请求分发到后端服务器,循环进行。
- IP Hash:根据客户端的IP地址进行哈希计算,确保相同的客户端IP始终被分发到同一台后端服务器,有助于保持会话一致性。
- 最小连接数(Least Connections):将请求发送到当前连接数最少的后端服务器,有助于分散负载。
- 权重(Weighted):为每个后端服务器分配权重,以控制请求的分发比例。
动静分离
- 静态文件服务:
- Nginx被广泛用作静态文件服务器,可以高效地处理和响应静态资源(如HTML、CSS、JavaScript、图片等)的请求。
- 静态资源通常是不变的,因此可以进行缓存,降低对后端服务器的请求次数。
- 反向代理动态请求:
- 动态请求(如PHP、Python、Java等应用生成的内容)通常会经过Nginx反向代理到应用服务器,由应用服务器处理请求并生成响应。
- 反向代理允许将动态请求和静态请求分开处理,提高了系统的灵活性和可维护性。
Nginx概念词汇表:名词解释 - NGINX (nginx-cn.net)
Nginx安装的三种方式
此处采用 Linux 安装,主机号为192.168.84.134
Linux CentOS 9 安装:Linux CentOS9安装配置-CSDN博客
YUM安装
使用 yum 包管理器安装
添加Nginx Repository:
sudo tee /etc/yum.repos.d/nginx.repo<<EOF
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/
gpgcheck=0
enabled=1
EOF
使用yum安装
sudo yum install -y nginx
启动nginx并设置开机自启动
#启动nginx服务
systemctl start nginx
#检查nginx服务是否启动
systemctl status nginx
#设置nginx为开机自启动
#停止nginx服务
systemctl stop nginx
浏览器访问 主机IP:80
会出现以下页面
nginx默认监听80端口
源码编译安装
源码编译安装,因为 Nginx 是C语言开发的软件
使用到工具wget
下载nginx源码并解压(找一个自己记得住的目录)
wget http://nginx.org/download/nginx-1.20.2.tar.gz
tar -zxvf nginx-1.20.2.tar.gz
cd nginx-1.20.2
配置编译选项
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-threads --with-stream --with-stream_ssl_module
编译安装
#编译
make
#安装
sudo make install
配置 Nginx 系统服务
sudo tee /usr/lib/systemd/system/nginx.service<<EOF
[Unit]
Description=nginx - high performance web server
Documentation=https://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP \$MAINPID
ExecStop=/bin/kill -s TERM \$MAINPID
[Install]
WantedBy=multi-user.target
EOF
启动 Nginx 并设置开机自启
#启动nginx服务
systemctl start nginx
#检查nginx服务是否启动
systemctl status nginx
#设置nginx为开机自启动
#停止nginx服务
systemctl stop nginx
docker安装
docker安装nginx
nginx是无状态应用,较为推荐采用容器化部署
Linux 安装docker:Docker上手指南-CSDN博客
docker安装简介
#下载依赖文件
sudo yum install -y yum-utils
#设置镜像仓库
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
#更新索引
yum makecache
#下载docker引擎
yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
#启动docker
systemctl start docker
#设置docker为开机自启动
systemctl enable docker
拉起nginx镜像
sudo docker pull nginx
查看nginx镜像
docker images
挂载本地 nginx 配置文件到nginx容器中
(此处使用 vim 编辑器 yum install -y vim
)
#创建nginx.conf文件,此处为示例目录/opt/nginx
mkdir -p /opt/nginx
vim /opt/nginx/nginx.conf
填写 nginx.conf 文件
(此处先填写为 nginx.conf 的初始化内容)
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2;
# listen [::]:443 ssl http2;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
启动 nginx 容器
docker run --name myNginx -v /opt/nginx/nginx.conf:/etc/nginx/nginx.conf:ro -p 80:80 -d nginx
#记得将挂载目录换为自己的本地nginx.conf的文件位置
#如果有需要代理的静态页面,则也需要挂载进nginx
查看nginx容器运行情况
docker ps
卸载Nginx
从宿主机删除
- 卸载nginx软件
#停止nginx服务
sudo systemctl stop nginx
#卸载nginx软件包
sudo yum remove nginx
- 删除nginx存留文件
[root@node4 nginx]# find / -type f -name "*nginx*" -delete
[root@node4 nginx]# find / -type d -name "*nginx*" -exec rm -rf {} +
[root@node4 nginx]# whereis nginx
nginx:
[root@node4 nginx]# find /nginx
find: ‘/nginx’: No such file or directory
删除nginx的docker容器与镜像
docker stop <nginx容器id>
docker rm <nginx容器id>
docker rm <nginx 镜像id>
Nginx配置文件
重点
查找 Nginx 配置文件命令:
find / -name nginx.conf
检查配置文件语法的命令:
nginx -t
重新加载 Nginx 配置文件的命令:
nginx -s reload
,或是重新启动Nginx杀死所有nginx进程的命令:
pkill -f nginx
Nginx 默认配置文件位置 /etc/nginx/nginx.conf
默认配置文件内容
user nginx; #设置Nginx Work进程的用户为"nginx"
worker_processes auto; #设置Nginx Work进程的数量为本机CPU核心的数量
error_log /var/log/nginx/error.log notice; #设置错误日志的路径 日志级别为notice
pid /var/run/nginx.pid; #设置Nginx主进程的PID文件路径为 "/var/run/nginx.pid"
#events块用于配置Nginx的时间处理
events {
worker_connections 1024; #设置Nginx Work进程可处理的最大连接数为1024
}
#http块用于配置Nginx的HTTP服务器
http {
include /etc/nginx/mime.types; #包含"mime.types"文件,该文件定义了MIME类型映射
default_type application/octet-stream; #设置默认的MIME类型为"application/octet-stream"
#定义名为"main"的日志格式,包括远程地址、远程用户、请求时间、请求URL、HTTP状态码、发送的字节数、Referer、User-Agent和X-Forwarded-For头
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#设置访问日志的路径为“/var/log/nginx/access.log”,日志格式为main
access_log /var/log/nginx/access.log main;
#开启sendfile功能,提高静态文件的传输性能
sendfile on;
#如果开启,将尝试在一个数据包中发送HTTP响应头,提高网络效率
#tcp_nopush on;
#设置keep-alive连接的超时时间为65秒
keepalive_timeout 65;
#如果开启,将对HTTP响应内容进行gzip压缩,减少传输的数据量
#gzip on;
#包含"/etc/nginx/conf.d/"目录下所有以".conf"结尾的配置文件,用于存储各个站点或服务的独立配置
include /etc/nginx/conf.d/*.conf;
#server块,用于定义一个虚拟主机或服务器
server {
#监听IPv4的80端口
listen 80;
#监听IPv6的80端口
listen [::]:80;
#设置服务器名称,服务器名称"_"表示匹配任意请求
server_name _;
#设置服务器根目录为“/usr/share/nginx/html”
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
#网站404错误时返回 /404.html 页面
error_page 404 /404.html;
#location块为空表示使用默认的处理方式
location = /404.html {
}
#与上同理
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
Work进程是实际处理请求的进程
其他设置:
worker_rlimit_nofile
: 设置Worker进程可以打开的最大文件描述符数量
use epoll;
: 使用epoll事件模型,提高高并发情况下的性能
multi_accept on;
: 允许Worker进程在收到一个新连接通知时尽可能多地接受连接
server_names_hash_bucket_size
: 设置服务器名称哈希表的桶大小,影响域名的处理速度
server_names_hash_max_size
: 设置服务器名称哈希表的最大大小
log_not_found on;
: 启用或禁用记录不存在的错误页面的日志
access_log off;
: 禁用访问日志
error_log off;
: 禁用错误日志
open_file_cache max=1000 inactive=20s;
: 设置打开文件缓存的最大数量和非活动时间
open_file_cache_valid 30s;
: 设置打开文件缓存的有效时间
open_file_cache_min_uses 2;
: 设置文件被访问多少次后才被缓存
open_file_cache_errors on;
: 是否缓存文件找不到等错误信息
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
: 设置需要压缩的MIME类型
gzip_vary on;
: 启用"Vary: Accept-Encoding"头,让代理服务器根据请求头判断是否应该缓存压缩版本
gzip_proxied any;
: 对所有代理请求启用压缩
gzip_comp_level 6;
: 设置压缩级别,1-9,数字越大压缩率越高,但CPU占用也越高
gzip_buffers 16 8k;
: 设置压缩缓冲区的数量和大小
gzip_http_version 1.1;
: 设置压缩使用的HTTP协议版本
ssl_session_cache shared:SSL:10m;
: 设置SSL会话缓存的类型和大小
ssl_session_timeout 10m;
: 设置SSL会话的超时时间
反向代理使用
此处使用CentOS 9操作系统,默认配有python环境
停止nginx服务
systemctl stop nginx
编写三个html页面
此处示例目录为
/home/fishpie/workspace/
- page1.html
<!DOCTYPE html>
<html>
<head>
<title>Page 1</title>
</head>
<body>
<h1>This is Page 1</h1>
<p>Served by port 8001</p>
</body>
</html>
启动python启动HTTP服务器,page1使用8001端口
[root@node4 page1]# python -m http.server 8001 &
- page2.html
<!DOCTYPE html>
<html>
<head>
<title>Page 2</title>
</head>
<body>
<h1>This is Page 2</h1>
<p>Served by port 8002</p>
</body>
</html>
启动python启动HTTP服务器,page2使用8002端口
[root@node4 page1]# python -m http.server 8002 &
- page3.html
<!DOCTYPE html>
<html>
<head>
<title>Page 3</title>
</head>
<body>
<h1>This is Page 3</h1>
<p>Served by port 8003</p>
</body>
</html>
启动python启动HTTP服务器,page3使用8003端口
[root@node4 page2]# python -m http.server 8003 &
更改nginx.conf配置文件
(如果找不到nginx.conf文件,可以使用查找命令 find / -name nginx.conf
)
vim /etc/nginx/nginx.conf
nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
upstream backend {
server localhost:8001;
server localhost:8002;
server localhost:8003;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://backend;
}
}
}
检查nginx语法是否正确
nginx -t
使用指定nginx运行使用的配置文件
nginx -c /etc/nginx/nginx.conf
负载均衡策略
此时访问浏览器: 主机IP
此处示例为我的虚拟机IP:http://192.168.84.134/
修改
upstream
块更改
nginx.conf
文件后记得重启nginx使其生效杀死所有nginx进程的命令:
pkill -f nginx
轮询
-
默认的负载均衡策略
-
请求按时间顺序逐一分配到不同的后端服务器
-
每个服务器处理相同数量的请求
-
适用于所有后端服务器性能相近的情况
在完成以上配置后
可以发现页面的访问顺序是依次进行,这就是默认的负载均衡策略:轮询
坏处是无视服务器繁忙情况(相当于写死的策略,不能进行动态规划)
加权轮询
-
在轮询策略基础上为每个后端服务器分配不同的权重
-
权重越高的服务器接收到的请求越多
-
适用于后端服务器性能不均衡,需要分配不同的请求负载的情况
修改 nginx.conf 中的 upstream
块
upstream backend {
server localhost:8001 weight=3;
server localhost:8002;
server localhost:8003 weight=2;
}
可以看出page1出现的次数明显多于page2
权重的设置需要根据实际情况设置
最少连接数策略
-
将请求分配到当前连接数最少的后端服务器
-
适用于后端服务器性能不均衡的情况,能够更好地利用服务器资源
-
避免将过多请求分配到繁忙的服务器上。
修改 nginx.conf 中的 upstream
块
upstream backend {
least_conn;
server backend1.com:8001;
server backend2.com:8002;
server backend3.com:8003;
}
这样就会将新的请求分配给此时处理请求最少的后端服务
最少连接数策略看似是负载均衡的理想解决方案,但是,当一个连接数相对少的后端服务在处理一个特别耗时的请求时(假设这个耗时是无法接受的),此时就不可以再把新的请求分配给这个后端服务(因为分配给它也是浪费时间)
加权最小连接数
-
在最少连接策略基础上,为每个后端服务器分配不同的权重
-
将请求分配到当前连接数最少的后端服务器,同时考虑服务器的权重
-
适用于后端服务器性能不均衡,需要分配不同的请求负载的情况
修改 nginx.conf 中的 upstream
块
upstream backend {
least_conn;
server backend1.com:8001 weigh=10;
server backend2.com:8002;
server backend3.com:8003 weight=5;
}
结合了最小连接数和权重两个因素,但是连接数是优先考虑的因素
例如在此处:
backend1.com:2个连接
backend2.com:1个连接
backend3.com:1个连接
虽然backend1.com此时权重最大,但是backend2.com和backend3.com的连接数少于backend1.com,所以Nginx会在backend2.com与backend3.com中做选择,又由于backend3.com的权重高于backend2.com(backend2.com权重默认为0),所以Nginx会优先将请求分配给backend3.com
IP哈希
-
根据客户端的IP地址计算哈希值,将请求分配到特定的后端服务器
-
来自同一个IP地址的请求总是被分配到同一个后端服务器
-
适用于需要将来自同一客户端的请求始终发送到同一服务器的情况,如会话保持。
修改 nginx.conf 的 upstream 块
upstream backend {
ip_hash;
server localhost:8001;
server localhost:8002;
server localhost:8003;
}
此时我这台主机的所有请求都只会由Nginx分配给page2
IP哈希策略也存在一些限制:
-
如果后端服务器的数量发生变化(添加或移除服务器),哈希分布可能会受到影响,导致一些请求被分配到不同的服务器
-
如果客户端使用了代理服务器或NAT(网络地址转换),多个客户端可能共享同一个IP地址,导致请求分配不均衡。
-
无法根据服务器的实际负载情况动态调整请求分配
随机策略
-
随机选择一个后端服务器来处理请求
-
可以指定服务器的权重,权重越高被选中的概率越大
-
适用于后端服务器性能相近,且无需考虑会话保持的情况
修改 nginx.conf 文件的 upstream 块
upstream backend {
random;
server localhost:8001;
server localhost:8002;
server localhost:8003;
}
完全随机模式,将请求随计发送给本机8001,8002,8003端口
随机模式还可以与其他模式组合
upstream backend {
random two least_conn;
server localhost:8001 weight=2;
server localhost:8002;
server localhost:8003 weight=3;
}
随机加权最小链接模式:在所有服务中随机选择两个,然后再根据加权最小连接数从这两个服务中选择请求接收方
自定义策略
-
可以根据特定的需求编写自己的负载均衡算法
-
可以使用Nginx的脚本语言(如Lua)或第三方模块来实现自定义策略
示例1:
假设有两组后端服务器:一组用于处理静态资源请求,另一组用于处理动态请求。我们希望将以/static/
开头的请求转发到静态资源服务器组,其他请求转发到动态请求服务器组
修改 nginx.conf 的 http 块
http {
upstream static_servers {
server static1.example.com;
server static2.example.com;
}
upstream dynamic_servers {
server dynamic1.example.com;
server dynamic2.example.com;
server dynamic3.example.com;
}
server {
listen 80;
server_name example.com;
location /static/ {
proxy_pass http://static_servers;
}
location / {
proxy_pass http://dynamic_servers;
}
}
}
示例2:
需要先安装启用
--with-http_lua_module
模块
根据请求的User-Agent头信息来选择后端服务器,将移动设备的请求转发到一组服务器,将桌面设备的请求转发到另一组服务器
- 创建Lua脚本文件
user_agent.lua
local _M = {}
local mobile_patterns = {
"Android",
"iPhone",
"iPad",
"Windows Phone"
}
function _M.init()
#-- 初始化代码,可以留空
end
function _M.is_mobile()
local user_agent = ngx.var.http_user_agent
if user_agent then
for _, pattern in ipairs(mobile_patterns) do
if string.find(user_agent, pattern, 1, true) then
return true
end
end
end
return false
end
return _M
- 修改 nginx.conf 文件中的 http 模块
http {
#移动设备的负载地址
upstream mobile_servers {
server mobile1.example.com;
server mobile2.example.com;
}
#桌面设备的负载地址
upstream desktop_servers {
server desktop1.example.com;
server desktop2.example.com;
server desktop3.example.com;
}
#改为lua脚本所在位置
lua_package_path "/path/to/lua/scripts/?.lua;;";
init_by_lua_block {
#选用所用脚本的名称
local ua = require("user_agent")
ua.init()
}
server {
listen 80;
server_name example.com;
location / {
set $backend '';
rewrite_by_lua_block {
local ua = require("user_agent")
if ua.is_mobile() then
ngx.var.backend = "mobile_servers"
else
ngx.var.backend = "desktop_servers"
end
}
proxy_pass http://$backend;
}
}
}
很容易出错,建议找 chatGPT 或 Claude 检查一遍
HTTPS
Nginx除了可以转发 http 请求外还可以转发 https 请求,使自己的网站变为“安全站点”
- http 默认请求端口是 80
- https 默认请求端口是 443
https协议请求需要使用有效 SSL 证书,这个可以在云服务器厂商那里免费搞到,此处不多赘述
需要注意的是,SSL证书的有效性依赖于主机的时间,务必保证自己主机时间的准确性,否则很可能导致SSL证书无法生效,wget 与 curl 命令后的url为 https 协议时,很可能也因为主机时间不一致导致使用问题
#检查系统时间
date
#使用ntpdate工具一键矫正系统时间
sudo yum install ntpdate
sudo ntpdate pool.ntp.org
ps:更多实用的系统工具:Linux常用工具(LTS)-CSDN博客
虚拟主机
虚拟主机可以在一台服务器上部署多个站点
修改
server
块
示例:
修改 nginx.conf 文件中 http或https 块中的 server 块
server {
listen 80;
server_name example.com;
location / {
root /home/fishpie/blog-web/blog;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location ^~ /api/ {
proxy_pass http://192.168.84.134:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 80;
server_name admin.emample.com;
location / {
root /home/fishpie/blog-web/admin;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location ^~ /api/ {
proxy_pass http://192.168.84.134:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
(仅为示例)
此时访问域名example.com
与它的子域名admin.emample.com
分别可以显示它们的主页与管理端页面,这两个站点部署在同一台服务器上
Nginx中文文档:NGINX - 高级负载均衡器、Web服务器、反向代理 | 弘协网络 (nginx-cn.net)
Nginx概念词汇表:名词解释 - NGINX (nginx-cn.net)