前言
在项目部署时,为了最大限度地提升资源访问的性能,我们会将静态资源请求和动态api
请求分开处理,承担这一工作的角色往往就是我们的nginx
服务。下面我们就要使用nginx
反向代理的功能来对动静资源进行请求分离,以帮助我们在真实生产环境能够更好地进行项目部署。
docker compose编写
在安装好docker
与docker compose
后,我们需要编写相关的docker-compose.yaml
文件,为启动nginx
服务做准备,一般我们会打开docker hub
以便我们能够更好地编写docker-compose.yaml
文件;
我们需要关注以下几个点:
1.nginx镜像版本;
2.需要暴露的端口号;
3.静态资源存放的文件夹路径;
4.nginx配置文件路径;
- 为了使用最稳定的
nginx
版本,建议使用stable
,不用考虑版本号,这样拉取的nginx
版本就是最新最稳定的,如果想要确定具体版本,我们也可以去nginx
官网查看最新stable
版本,并在配置中指定该版本号即可,那么我们在文章中就使用最新的stable
版本1.24.0
;
- 关于
nginx
暴露的端口号,根据http
协议默认的几个端口号,我们一般会选择默认暴露80
端口,如果需要https
的话,那么需要暴露443
端口;
- 我们从
docker hub
可以找到容器内部的静态资源文件夹路径为/usr/share/nginx/html
,我们需要考虑把该文件夹路径映射到宿主机对应的某个路径,我一般会使用相对路径./nginx/www
; - 想要使用反向代理功能,我们需要通过
nginx
配置文件来启动,从docker hub
可以找到配置文件nginx.conf
路径为:/etc/nginx/nginx.conf
,我们需要把这个文件映射到我们的宿主机,以便我们能够方便地修改配置,一般我会映射到相对路径./nginx/conf/nginx.conf
;
所以,最终我们编写好的docker-compose.yaml
文件为:
version: "3"
services:
nginx:
image: nginx:1.24.0
ports:
- "80:80"
volumes:
# 静态资源文件
- "./nginx/www:/usr/share/nginx/html"
# 配置文件
- "./nginx/conf/nginx/conf:/etc/nginx/nginx.conf"
nginx.conf配置
在上面的docker-compose.yaml
文件编写好后,我们还不能马上启动nginx
服务,因为我们的配置文件nginx.conf
还没有准备好,我们在编写配置文件时需要关注以下两件事情:
1.静态资源请求的路由策略;
2.动态资源请求的路由策略以及反向代理的目标服务;
-
我们的静态资源一般都是
css
、js
、html
、图片、字体等资源,像这类资源请求我们可以在nginx
中使用后缀匹配的方式来路由;location ~* .(css|js|html|xhtml|gif|jpg|jpeg|png|ico)$ { root /usr/share/nginx/html; index index.html index.xhtml; }
上述配置只是一个示例,如果有没有覆盖全的后缀请自行补充;
-
我们已经把静态资源全部处理完毕,那么剩下的就是动态请求了;另外我们还需要配置
nginx
把该动态请求路由到哪个目标服务上去;假如我们的目标服务为
http://192.168.2.218:8080
location / { proxy_pass http://192.168.2.218:8080; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Scheme $scheme; }
如果咱们的目标服务为集群部署,那么我们可以这样配置:
location / { proxy_pass http://target_servers; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Scheme $scheme; }
另外我们需要额外再配置一个
upstream
:upstream target_servers { server 192.168.2.218:8080; server 192.168.2.219:8080; server 192.168.2.220:8080; }
这样的话,我们就可以将动态请求分发给上述几个目标服务;
需要注意的是,如果目标服务与
nginx
实例在同一个宿主机,不能在nginx.conf
中写127.0.0.1
或localhost
,而应该使用局域网ip
,并保证nginx
实例能够ping
通该ip
或域名;
所以,我们最终的配置文件nginx.conf
为:
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 2048;
}
http {
include 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 logs/access.log main;
sendfile on;
# tcp_nopush on;
keepalive_timeout 65;
# gzip压缩功能设置
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 6;
gzip_types text/html text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
gzip_vary on;
# http_proxy 设置
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 75;
proxy_send_timeout 75;
proxy_read_timeout 75;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
# 设定负载均衡后台服务器列表
upstream target_servers {
server 192.168.2.218:8080;
server 192.168.2.219:8080;
server 192.168.2.220:8080;
}
# 很重要的虚拟主机配置
server {
listen 80;
server_name itoatest.example.com;
root /apps/oaapp;
charset utf-8;
access_log logs/host.access.log main;
# 静态资源路由
location ~* .(css|js|html|xhtml|gif|jpg|jpeg|png|ico)$ {
root /usr/share/nginx/html;
index index.html index.xhtml;
}
# 动态请求
location / {
proxy_pass http://target_servers;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
}
}
}
启动nginx服务
我们在上述两个文件都准备好后,需要将nginx.conf
文件放到相对路径./nginx/conf
中,另外我们需要将静态资源文件放进相对路径./nginx/www
中,最后我们使用启动命令docker-compose up -d
启动nginx
服务,最终我们的请求就通过nginx
实现了动静分离的功能了;