Update configmap.yaml

pull/10/head
plhw57tbe 4 months ago
parent 9f128c4f32
commit c70e63922d

@ -1,119 +1,124 @@
apiVersion: v1
kind: ConfigMap
apiVersion: v1 # Kubernetes API版本v1为稳定版本
kind: ConfigMap # 资源类型为ConfigMap用于存储非敏感配置数据
metadata:
name: web-nginx-config
namespace: djangoblog
data:
nginx.conf: |
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
name: web-nginx-config # ConfigMap名称标识该Nginx配置资源
namespace: djangoblog # 所属命名空间用于资源隔离对应djangoblog应用
data: # 配置数据,键为文件名,值为文件内容
nginx.conf: | # Nginx主配置文件
user nginx; # Nginx进程运行的用户
worker_processes auto; # 工作进程数auto表示按CPU核心数自动分配
error_log /var/log/nginx/error.log notice; # 错误日志路径及级别notice级别
pid /var/run/nginx.pid; # Nginx进程PID文件路径
events {
worker_connections 1024;
multi_accept on;
use epoll;
events { # 事件处理配置块
worker_connections 1024; # 每个工作进程最大连接数
multi_accept on; # 允许工作进程同时接受多个新连接
use epoll; # 使用epoll I/O模型Linux下高效事件驱动模型
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
http { # HTTP核心配置块
include /etc/nginx/mime.types; # 引入MIME类型映射文件识别文件类型
default_type application/octet-stream; # 默认MIME类型未知类型时使用
# 定义日志格式命名为main
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; # 访问日志路径使用main格式
access_log /var/log/nginx/access.log main;
sendfile on; # 启用sendfile系统调用高效传输文件
keepalive_timeout 65; # 长连接超时时间65秒
gzip on; # 启用gzip压缩减少传输数据量
gzip_disable "msie6"; # 对IE6浏览器禁用gzip兼容性处理
sendfile on;
keepalive_timeout 65;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 8;
gzip_buffers 16 8k;
gzip_http_version 1.1;
# gzip压缩补充配置
gzip_vary on; # 启用Vary: Accept-Encoding响应头告知代理缓存压缩/非压缩版本)
gzip_proxied any; # 对所有代理请求启用压缩
gzip_comp_level 8; # 压缩级别1-98为较高压缩率
gzip_buffers 16 8k; # 压缩缓冲区大小16个8k缓冲区
gzip_http_version 1.1; # 仅对HTTP/1.1及以上版本启用压缩
# 需压缩的文件类型文本、JS、CSS、图片等
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
# Include server configurations
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/conf.d/*.conf; # 引入其他服务器配置文件
}
djangoblog.conf: |
server {
server_name lylinux.net;
root /code/djangoblog/collectedstatic/;
listen 80;
keepalive_timeout 70;
location /static/ {
expires max;
alias /code/djangoblog/collectedstatic/;
djangoblog.conf: | # lylinux.net域名的Nginx站点配置
server { # 处理lylinux.net域名的服务配置
server_name lylinux.net; # 绑定的主域名
root /code/djangoblog/collectedstatic/; # 网站根目录(静态文件目录)
listen 80; # 监听80端口HTTP
keepalive_timeout 70; # 该站点长连接超时时间
location /static/ { # 处理静态文件请求
expires max; # 静态文件缓存有效期设为最大(长期缓存)
alias /code/djangoblog/collectedstatic/; # 静态文件实际路径
}
# 处理特定静态文件如robots.txt、网站验证文件等
location ~* (robots\.txt|ads\.txt|favicon\.ico|favion\.ico|crossdomain\.xml|google93fd32dbd906620a\.html|BingSiteAuth\.xml|baidu_verify_Ijeny6KrmS\.html)$ {
root /resource/djangopub;
expires 1d;
access_log off;
error_log off;
root /resource/djangopub; # 这些文件的根目录
expires 1d; # 缓存1天
access_log off; # 关闭访问日志
error_log off; # 关闭错误日志
}
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
location / { # 处理其他所有请求反向代理到Django
# 设置代理请求头(传递客户端信息给后端)
proxy_set_header X-Real-IP $remote_addr; # 客户端真实IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 代理链IP列表
proxy_set_header Host $http_host; # 原始请求Host
proxy_set_header X-NginX-Proxy true; # 标识经Nginx代理
proxy_redirect off; # 禁用代理重定向
# 若请求文件不存在反向代理到Django服务djangoblog为K8s内部服务名
if (!-f $request_filename) {
proxy_pass http://djangoblog:8000;
break;
}
}
}
server {
server_name www.lylinux.net;
listen 80;
return 301 https://lylinux.net$request_uri;
server { # 处理www.lylinux.net域名重定向配置
server_name www.lylinux.net; # 绑定的www子域名
listen 80; # 监听80端口
return 301 https://lylinux.net$request_uri; # 永久重定向到主域名HTTPS地址
}
resource.lylinux.net.conf: |
resource.lylinux.net.conf: | # resource.lylinux.net子域名的配置资源服务器
server {
index index.html index.htm;
server_name resource.lylinux.net;
root /resource/;
index index.html index.htm; # 默认索引文件
server_name resource.lylinux.net; # 绑定的资源子域名
root /resource/; # 资源文件根目录
location /djangoblog/ {
alias /code/djangoblog/collectedstatic/;
location /djangoblog/ { # 映射Django静态文件路径
alias /code/djangoblog/collectedstatic/; # 实际静态文件路径
}
access_log off;
error_log off;
include lylinux/resource.conf;
access_log off; # 关闭访问日志
error_log off; # 关闭错误日志
include lylinux/resource.conf; # 引入通用资源配置
}
lylinux.resource.conf: |
expires max;
access_log off;
log_not_found off;
add_header Pragma public;
add_header Cache-Control "public";
add_header "Access-Control-Allow-Origin" "*";
lylinux.resource.conf: | # 通用资源配置(被资源服务器引用)
expires max; # 资源缓存有效期设为最大
access_log off; # 关闭访问日志
log_not_found off; # 关闭文件未找到的错误日志
add_header Pragma public; # 缓存控制头(告知客户端可缓存)
add_header Cache-Control "public"; # 缓存控制头(公开可缓存)
add_header "Access-Control-Allow-Origin" "*"; # 允许跨域访问(所有域名)
---
apiVersion: v1
kind: ConfigMap
apiVersion: v1 # Kubernetes API版本
kind: ConfigMap # 资源类型为ConfigMap存储环境变量
metadata:
name: djangoblog-env
namespace: djangoblog
data:
DJANGO_MYSQL_DATABASE: djangoblog
DJANGO_MYSQL_USER: db_user
DJANGO_MYSQL_PASSWORD: db_password
DJANGO_MYSQL_HOST: db_host
DJANGO_MYSQL_PORT: db_port
DJANGO_REDIS_URL: "redis:6379"
DJANGO_DEBUG: "False"
MYSQL_ROOT_PASSWORD: db_password
MYSQL_DATABASE: djangoblog
MYSQL_PASSWORD: db_password
DJANGO_SECRET_KEY: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
name: djangoblog-env # ConfigMap名称标识Django环境变量配置
namespace: djangoblog # 所属命名空间(与应用一致)
data: # 环境变量键值对
DJANGO_MYSQL_DATABASE: djangoblog # Django连接的MySQL数据库名
DJANGO_MYSQL_USER: db_user # MySQL登录用户名
DJANGO_MYSQL_PASSWORD: db_password # MySQL登录密码
DJANGO_MYSQL_HOST: db_host # MySQL服务地址K8s内部服务名或IP
DJANGO_MYSQL_PORT: db_port # MySQL服务端口
DJANGO_REDIS_URL: "redis:6379" # Redis服务地址及端口
DJANGO_DEBUG: "False" # Django调试模式生产环境关闭
MYSQL_ROOT_PASSWORD: db_password # MySQL root用户密码用于初始化
MYSQL_DATABASE: djangoblog # 初始化的MySQL数据库名
MYSQL_PASSWORD: db_password # MySQL普通用户密码与Django配置一致
DJANGO_SECRET_KEY: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx # Django加密密钥用于会话、CSRF等
Loading…
Cancel
Save