1. Nginx 简介
Nginx(发音为“Engine-X”)是一个开源的高性能 HTTP 和反向代理服务器,同时也是一个 IMAP/POP3/SMTP 代理服务器。由俄罗斯程序员 Igor Sysoev 创建。它以其高稳定性、丰富的功能集、简单的配置和低资源消耗而闻名。
2. 静态站点
需要配置静态站点,即 html 文件和一堆静态资源
举例来说:如果所有的静态资源都放在了 /app/dist
目录下,我们只需要在 nginx.conf
中指定首页以及这个站点的 host 即可。
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/javascript image/jpeg image/gif image/png;
gzip_vary on;
server {
listen 80;
server_name static.zp.cn;
location / {
root /app/dist;
index index.html;
#转发任何请求到 index.html
}
}
}
然后,添加 HOST:
127.0.0.1 static.zp.cn
此时,在本地浏览器访问 static.zp.cn ,就可以访问静态站点了。
3. 网站有多个 webapp 的配置
假如 www.helloworld.com
站点有好几个 webapp,product(产品)、admin(用户中心)。访问这些应用的方式通过上下文(context)来进行区分
www.helloworld.com/product/
www.helloworld.com/admin/
http 的默认端口号是 80,如果在一台服务器上同时启动这几个 webapp 应用,都用 80 端口,肯定是不成的。所以,这几个应用需要分别绑定不同的端口号。
那么,问题来了,用户在实际访问 www.helloworld.com
站点时,访问不同 webapp,总不会还带着对应的端口号去访问吧。所以,需要用到反向代理来做处理。
http {
#此处省略一些基本配置
upstream product_server{
server www.helloworld.com:8081;
}
upstream admin_server{
server www.helloworld.com:8082;
}
upstream finance_server{
server www.helloworld.com:8083;
}
server {
#此处省略一些基本配置
#默认指向product的server
location / {
proxy_pass http://product_server;
}
location /product/{
proxy_pass http://product_server;
}
location /admin/ {
proxy_pass http://admin_server;
}
location /finance/ {
proxy_pass http://finance_server;
}
}
}
4. 搭建文件服务器
适用于团队需要归档一些数据或资料
- 将 autoindex 开启可以显示目录,默认不开启。
- 将 autoindex_exact_size 开启可以显示文件的大小。
- 将 autoindex_localtime 开启可以显示文件的修改时间。
- root 用来设置开放为文件服务的根路径。
- charset 设置为
charset utf-8;
,可以避免中文乱码问题
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
charset utf-8;
listen 9050 default_server;
listen [::]:9050 default_server;
server_name _;
root /path/to/your/directory; # 修改为你要分享的文件所在的目录的路径
location / {
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
}
}
}
5. 反向代理
反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。
简单来说就是真实的服务器不能直接被外部网络访问,所以需要一台代理服务器,而代理服务器能被外部网络访问的同时又跟真实服务器在同一个网络环境,当然也可能是同一台服务器,端口不同而已。
5.1. http反向代理
反向代理通过proxy_pass
指令来实现
根据HTTP请求头中的Host字段区分域名,并将请求转发至内网不同服务器
server {
listen 80;
server_name domain1.com;
location / {
proxy_pass http://192.168.1.100:80; # 内网服务器A
}
}
server {
listen 80;
server_name domain2.com;
location / {
proxy_pass http://192.168.1.101:80; # 内网服务器B
}
}
5.2. https反向代理
使用 nginx 配置 https 需要知道几点:
- HTTPS 的固定端口号是 443,不同于 HTTP 的 80 端口
- SSL 标准需要引入安全证书,所以在 nginx.conf 中你需要指定证书和它对应的 key
其他和 http 反向代理基本一样,只是在 Server
部分配置有些不同。
6. 负载均衡
网站在实际运营过程中,大部分都是以集群的方式运行,这时需要使用负载均衡来分流。
假设这样一个应用场景:将应用部署在 192.168.1.11:80、192.168.1.12:80、192.168.1.13:80 三台 linux 环境的服务器上。网站域名叫 www.helloworld.com
,公网IP 为 192.168.1.11。在公网 IP 所在的服务器上部署 nginx,对所有请求做负载均衡处理
6.1. 轮询
upstream bck_testing_01 {
# 默认所有服务器权重为 1
server 192.168.250.220:8080
server 192.168.250.221:8080
server 192.168.250.222:8080
}
6.2. 加权轮询
upstream bck_testing_01 {
server 192.168.250.220:8080 weight=3
server 192.168.250.221:8080 # default weight=1
server 192.168.250.222:8080 # default weight=1
}
6.3. 最少连接
upstream bck_testing_01 {
least_conn;
# with default weight for all (weight=1)
server 192.168.250.220:8080
server 192.168.250.221:8080
server 192.168.250.222:8080
}
6.4. 加权最少连接
upstream bck_testing_01 {
least_conn;
server 192.168.250.220:8080 weight=3
server 192.168.250.221:8080 # default weight=1
server 192.168.250.222:8080 # default weight=1
}
6.5. IP Hash
upstream bck_testing_01 {
ip_hash;
# with default weight for all (weight=1)
server 192.168.250.220:8080
server 192.168.250.221:8080
server 192.168.250.222:8080
}
6.6. 普通Hash
upstream bck_testing_01 {
hash $request_uri;
# with default weight for all (weight=1)
server 192.168.250.220:8080
server 192.168.250.221:8080
server 192.168.250.222:8080
}