本文最后更新于 646 天前,其中的信息可能已经有所发展或是发生改变。
Docker部署和使用Nginx
- 拉取镜像
docker pull nginx
- 创建挂载目录
mkdir -p /home/docker/nginx/conf
mkdir -p /home/docker/nginx/log
mkdir -p /home/docker/nginx/html
- 从容器中复制nginx.conf及conf.d文件夹
# 生成容器
docker run --name nginx -p 9003:80 -d nginx
# 将容器nginx.conf文件复制到宿主机
docker cp nginx:/etc/nginx/nginx.conf /home/docker/nginx/conf/nginx.conf
# 将容器conf.d文件夹下内容复制到宿主机
docker cp nginx:/etc/nginx/conf.d /home/docker/nginx/conf/conf.d
# 将容器html文件夹下内容复制到宿主机
docker cp nginx:/usr/share/nginx/html /home/docker/nginx/html
- 删除原nginx容器,改用挂载方式启动
docker run -p 9003:80 --name nginx -d \
-v /home/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /home/docker/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /home/docker/nginx/log:/var/log/nginx \
-v /home/docker/nginx/html:/usr/share/nginx/html \
nginx
Nginx的配置文件
nginx.conf :
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
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;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
include /etc/nginx/conf.d/*.conf
表示引入conf.d目录下的以.conf为结尾的配置文件
conf.d/default.conf :
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
server块:
listen:表示Nginx监听的端口号
server_name:表示监听地址
location块:
root:去上述路径找静态资源
index:去上述路径找index.html
Nginx反向代理
只需要修改conf.d/default.conf:
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
proxy_pass #目标地址;
}
}
表示当请求发向80端口,会自动转发到proxy_pass后的目标地址
关于Nginx的location路径映射
符号 | 含义 |
---|---|
= | 开头表示精确匹配 |
^~ | 开头表示 uri 以某个常规字符串开头,理解为匹配 url 路径即可。nginx 不对 url 做编码,因此请求为/static/20%/aa ,可以被规则^~ /static/ /aa 匹配到(注意是空格) |
~ | 开头表示区分大小写的正则匹配 |
~ * | 开头表示不区分大小写的正则匹配 |
/ | 通用匹配,任何请求都会匹配到 |
多个 location 配置的情况下匹配顺序为
- 首先匹配
=
- 其次匹配
^~
- 其次是按文件中顺序的正则匹配
- 最后是交给 / 通用匹配
- 当有匹配成功时候,停止匹配,按当前匹配规则处理请求
举例:
location = / {
#规则A
}
location = /login {
#规则B
}
location ^~ /static/ {
#规则C
}
location ~ \.(gif|jpg|png|js|css)$ {
#规则D
}
location ~* \.png$ {
#规则E
}
location / {
#规则F
}
那么产生的效果如下:
访问根目录 /, 比如 http://localhost/ 将匹配规则 A
访问 http://localhost/login 将匹配规则 B,http://localhost/register 则匹配规则 F
访问 http://localhost/static/a.html 将匹配规则 C
访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配规则 D和规则 E,但是规则 D 顺序优先,规则 E不起作用,而 http://localhost/static/c.png则优先匹配到规则 C
访问 http://localhost/a.PNG 则匹配规则 E,而不会匹配规则 D,因为规则 E 不区分大小写
访问 http://localhost/category/id/1111 则最终匹配到规则 F,因为以上规则都不匹配,这个时候应该是 nginx 转发请求给后端应用服务器,比如 FastCGI(PHP),tomcat(jsp),nginx 作为反向代理服务器存在
参考:linux Nginx配置篇:location的匹配规则(附测试验证过程)_Etyero的博客-CSDN博客
nginx之location的匹配规则 – 江飞劫 – 博客园 (cnblogs.com)
Nginx负载均衡
Nginx为我们默认提供了三种负载均衡的策略:
1、轮询:
将客户端发起的请求平均分配给每一台服务器
2、权重:
将客户端的请求根据服务器的权重值不同,分配不同的数量
3、ip_hash
基于发起请求的客户端的ip地址,将请求发送给不同的服务器
轮询:
upstream my-server{
server ip:port;
server ip:port;
server ip:port;
}
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
proxy_pass http://upstream的名字/;
}
}
权重:
upstream my-server{
server ip:port weight=权重比例;
server ip:port weight=权重比例;
server ip:port weight=权重比例;
}
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
proxy_pass http://upstream的名字/;
}
}
ip_hash
upstream my-server{
ip_hash;
server ip:port;
server ip:port;
server ip:port;
}
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
proxy_pass http://upstream的名字/;
}
}
Nginx动静分离
动态资源代理
#配置如下
location / {
proxy_pass 路径;
}
静态资源代理
配置如下
location / {
root 静态资源路径;
index 默认访问路径下的什么资源
autoindex on; #展示静态资源的全部内容,以列表的形式展开
}