问题
最近需要自定义一个nginx发布静态文件和反向代理后台接口。
步骤
这里使用的nginx:1.26.2-alpine镜像(2024.10),现在这个时间点,这个版本的alpine和nginx版本都是最稳定的版本。
nginx.conf
这里需要自定义下/etc/nginx/nginx.conf
,具体内容如下:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
# pid文件使用临时目录
pid /tmp/nginx.pid;
events {
worker_connections 1024;
}
http {
# 反向代理的集群
upstream xxxx-gateway {
server xxx.xxx.xxx.67:8080;
}
# 临时文件使用临时目录
client_body_temp_path /tmp/client_temp;
proxy_temp_path /tmp/proxy_temp_path;
fastcgi_temp_path /tmp/fastcgi_temp;
uwsgi_temp_path /tmp/uwsgi_temp;
scgi_temp_path /tmp/scgi_temp;
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;
}
这里主要nginx生成的pid文件和临时文件,都使用到tmp临时目录;还有准备一个就是反向代理后台服务api接口服务集群。
default.conf
这里需要自定义下/etc/nginx/conf.d/default.conf
,具体内容如下:
server {
listen 8082;
listen [::]:8082;
# 隐藏nginx版本号
server_tokens off;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
# 反向代理代理配置
location /api/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://xxxx-gateway/;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
这里主要就是设置nginx服务端口;以及使用到的/api
调用后台接口服务的反向代理设置;还有一个静态前端部署文件配置。
Dockerfile
FROM nginx:1.26.2-alpine
# 配置文件需要修改
COPY ./nginx.conf /etc/nginx/nginx.conf
COPY ./default.conf /etc/nginx/conf.d/default.conf
# 修改 Nginx 相关目录的权限
RUN chown -R nginx:nginx /var/cache/nginx /var/run /var/log/nginx /etc/nginx/conf.d /usr/share/nginx/html
# 切换到非 root 用户
USER nginx:nginx
# 使用高端口以避免权限问题
EXPOSE 8082
# 启动 Nginx
CMD ["nginx", "-g", "daemon off;"]
注意这里使用的是nginx用户和nginx用户组,这个镜像自动这个nginx用户和nginx用户组。不要直接使用root账号,启动nginx容器。
手动构建
# docker正常构建镜像
docker build -t xxxx-nginx .
# docker构建x86镜像
docker buildx build --platform linux/amd64 -t xxxx-nginx .
# docker导出离线镜像
docker save -o xxxx-nginx.tar xxxx-nginx
# docker导入离线镜像
docker load -i xxxx-nginx.tar
# docker启动服务
docker run -d --name xxxx-nginx -v ~/xxx/nginx/html:/usr/share/nginx/html -p 8082:8082 xxxx-nginx
# 添加端口规则
sudo firewall-cmd --zone=public --add-port=8082/tcp --permanent
sudo firewall-cmd --reload
到这里基本就结束了,上述包括手动打镜像,导出镜像,导入离线镜像,docker运行这个自定义nginx容器,并配置防火墙。
总结
基于nginx官方docker镜像,处理静态文件部署和反向代理接口,还是停简单的。羡慕,云原生的开发环境,这些事情都不用处理。