Press "Enter" to skip to content

在Docker容器中为NGINX增加配置脚本

目前有个java项目,前后端分离的,他有一个前端web工程。

我们通常的web打包方法是给前端项目打包到一个NGINX容器中去,然后给NGINX配置特定的请求,比如 api开头的请求,转发到后端项目去。

我们这个后端项目可能测试环境和生产环境的地址不一样,我们希望在部署的时候通过环境变量来注入到这个NGINX中去,实现部署时,动态的告诉NGINX应该将api转发到哪里去。

Dockerfile文件

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
FROM nginx:latest
# NGINX镜像版本必须要在 1.19以上
# 将前端打包目录放置到NGINX前端目录
ADD dist /usr/share/nginx/html/
# 将本地NGINX配置文件放置到NGINX配置目录
ADD nginx.conf /etc/nginx
# 将我们自定义的启动器配置环境变量脚本放置于NGINX启动脚本目录
ADD start.sh /docker-entrypoint.d/
# 给启动脚本执行权限
RUN chmod a+x /docker-entrypoint.d/start.sh
EXPOSE 80
FROM nginx:latest # NGINX镜像版本必须要在 1.19以上 # 将前端打包目录放置到NGINX前端目录 ADD dist /usr/share/nginx/html/ # 将本地NGINX配置文件放置到NGINX配置目录 ADD nginx.conf /etc/nginx # 将我们自定义的启动器配置环境变量脚本放置于NGINX启动脚本目录 ADD start.sh /docker-entrypoint.d/ # 给启动脚本执行权限 RUN chmod a+x /docker-entrypoint.d/start.sh EXPOSE 80
FROM nginx:latest
# NGINX镜像版本必须要在 1.19以上

# 将前端打包目录放置到NGINX前端目录
ADD dist /usr/share/nginx/html/

# 将本地NGINX配置文件放置到NGINX配置目录
ADD nginx.conf /etc/nginx

# 将我们自定义的启动器配置环境变量脚本放置于NGINX启动脚本目录
ADD start.sh /docker-entrypoint.d/

# 给启动脚本执行权限
RUN chmod a+x /docker-entrypoint.d/start.sh

EXPOSE 80

NGINX配置文件

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
worker_processes auto;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
# this is necessary for us to be able to disable request buffering in all cases
proxy_http_version 1.1;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
server {
listen 80;
# disable any limits to avoid HTTP 413 for large image uploads
client_max_body_size 0;
root /usr/share/nginx/html;
location / {
try_files $uri /index.html;
}
location /api {
rewrite ^/api(.*) $1 break;
proxy_pass http://BACKEND_SERVICE;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffering off;
proxy_request_buffering off;
}
}
}
worker_processes auto; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; # this is necessary for us to be able to disable request buffering in all cases proxy_http_version 1.1; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; server { listen 80; # disable any limits to avoid HTTP 413 for large image uploads client_max_body_size 0; root /usr/share/nginx/html; location / { try_files $uri /index.html; } location /api { rewrite ^/api(.*) $1 break; proxy_pass http://BACKEND_SERVICE; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_buffering off; proxy_request_buffering off; } } }
worker_processes auto;

events {
  worker_connections 1024;
}

http {
  include       mime.types;
  default_type  application/octet-stream;

  # this is necessary for us to be able to disable request buffering in all cases
  proxy_http_version 1.1;

  sendfile        on;
  #tcp_nopush     on;

  #keepalive_timeout  0;
  keepalive_timeout  65;


  server {
    listen 80;

    # disable any limits to avoid HTTP 413 for large image uploads
    client_max_body_size 0;

    root /usr/share/nginx/html;

    location / {
      try_files $uri /index.html;
    }

    location /api {
      rewrite ^/api(.*) $1  break;
      proxy_pass http://BACKEND_SERVICE;
      proxy_set_header Host $http_host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_buffering off;
      proxy_request_buffering off;
    }
  }
}

环境变量配置脚本start.sh

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#!/bin/bash
# 替换nginx后端服务地址
sed -i "s/BACKEND_SERVICE/$BACKEND_SERVICE/g" /etc/nginx/nginx.conf
echo ":替换后端服务地址为:$BACKEND_SERVICE"
#!/bin/bash # 替换nginx后端服务地址 sed -i "s/BACKEND_SERVICE/$BACKEND_SERVICE/g" /etc/nginx/nginx.conf echo ":替换后端服务地址为:$BACKEND_SERVICE"
#!/bin/bash

# 替换nginx后端服务地址
sed -i "s/BACKEND_SERVICE/$BACKEND_SERVICE/g" /etc/nginx/nginx.conf
echo ":替换后端服务地址为:$BACKEND_SERVICE"

现在你只需要在部署前端容器的时候,通过环境变量BACKEND_SERVICE 来配置后端服务地址即可。

需要注意的是,你的NGINX镜像版本必须是在1.19以上才支持docker-entrypoint.d配置目录。

参考资料:

https://stackoverflow.com/questions/40608055/running-a-bash-script-before-startup-in-an-nginx-docker-container

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注