Nginx 在 dockerized 时拒绝读取自定义 nginx.config
Nginx refuses to read custom nginx.config when dockerized
我创建了一个带有简单代理的自定义 nginx.conf 文件,并将其放在我的项目的根目录中。
nginx.conf
user www-data;
worker_processes auto;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
#include /etc/nginx/sites-enabled/*;
server { # simple reverse-proxy
root /var/www/html;
listen 80;
# pass requests for dynamic content to rails/turbogears/zope, et al
location /test1/ {
proxy_pass http://dumy/test1/;
}
location /test2/ {
proxy_pass http://dumy/test2/;
}
}
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
当我 运行 我的本地 nginx 服务器 (ubuntu) 一切正常。代理正常工作。
当我尝试通过 docker 容器 运行 它时,无论我做什么,代理都不起作用。
我的 Dockerfile:
FROM node:alpine AS builder
WORKDIR '/app'
COPY package.json .
RUN npm install
COPY . .
RUN npm run prod
FROM nginxinc/nginx-unprivileged
COPY --from=builder /app/dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 8080
CMD ["nginx","-g", "daemon off;", "-c", "/etc/nginx/nginx.conf"]
我必须使用 nginxinc/nginx-unprivileged 因为该应用程序最终将部署在 openshift 中。
上面的 docker 文件将创建 docker 图像。
当我运行:
docker run -p 8080:8080 <image>
容器将被创建并启动。但是无论我做什么,它都会从默认配置开始。
这就是我 运行
得到的结果
nginx -T
容器内:
# configuration file /etc/nginx/conf.d/default.conf:
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#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.conf 文件被完全忽略了。就像
"-c", "/etc/nginx/nginx.conf"
从 Dockerfile 中的 cmd 永远不会 运行s。
我试图在 docker 运行 中传递命令,但没有成功。
你能帮帮我吗?
经过大量的反复试验,我终于成功完成了这项工作。首先将 Dockerfile 中的图像从:nginxinc/nginx-unprivileged 更改为 nginx:alpine
其次,给openshift里面的用户适当的权限。 运行 :
oc adm policy add-scc-to-user anyuid system:serviceaccount:<<NAMESPACE>>:default
接下来应在项目的根目录中创建一个 default.conf 文件,并且在服务器 {} 对象中具有与自定义 nginx.conf 文件相同的内容。最后应该对所有文件进行正确的配置。
default.conf:
server { # simple reverse-proxy
root /usr/share/nginx/html;
listen 80;
# pass requests for dynamic content to rails/turbogears/zope, et al
location /test1/ {
proxy_pass http://dumy/test1/;
}
location /test2/ {
proxy_pass http://dumy/test2/;
}
}
nginx.conf:
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
#include /etc/nginx/sites-enabled/*;
server { # simple reverse-proxy
root /var/www/html;
listen 80;
# pass requests for dynamic content to rails/turbogears/zope, et al
location /test1/ {
proxy_pass http://dumy/test1/;
}
location /test2/ {
proxy_pass http://dumy/test2/;
}
}
}
Docker 文件:
FROM node:alpine AS builder
WORKDIR '/app'
COPY package.json .
RUN npm install
COPY . .
RUN npm run prod
FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/
COPY default.conf /etc/nginx/conf.d/
RUN chgrp -R root /var/cache/nginx /var/run /var/log/nginx && \
chmod -R 770 /var/cache/nginx /var/run /var/log/nginx
EXPOSE 8080
CMD ["nginx","-g", "daemon off;"]
请记住,在 default.conf 和 nginx.conf 文件中,应更改服务器对象内的根目录以匹配静态文件 html 文件的路径。在我的例子中 docker 内的路径是:/usr/share/nginx/html 所以整个根变量应该是:
root /usr/share/nginx/html;
我创建了一个带有简单代理的自定义 nginx.conf 文件,并将其放在我的项目的根目录中。
nginx.conf
user www-data;
worker_processes auto;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
#include /etc/nginx/sites-enabled/*;
server { # simple reverse-proxy
root /var/www/html;
listen 80;
# pass requests for dynamic content to rails/turbogears/zope, et al
location /test1/ {
proxy_pass http://dumy/test1/;
}
location /test2/ {
proxy_pass http://dumy/test2/;
}
}
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
当我 运行 我的本地 nginx 服务器 (ubuntu) 一切正常。代理正常工作。
当我尝试通过 docker 容器 运行 它时,无论我做什么,代理都不起作用。
我的 Dockerfile:
FROM node:alpine AS builder
WORKDIR '/app'
COPY package.json .
RUN npm install
COPY . .
RUN npm run prod
FROM nginxinc/nginx-unprivileged
COPY --from=builder /app/dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 8080
CMD ["nginx","-g", "daemon off;", "-c", "/etc/nginx/nginx.conf"]
我必须使用 nginxinc/nginx-unprivileged 因为该应用程序最终将部署在 openshift 中。 上面的 docker 文件将创建 docker 图像。
当我运行:
docker run -p 8080:8080 <image>
容器将被创建并启动。但是无论我做什么,它都会从默认配置开始。
这就是我 运行
得到的结果nginx -T
容器内:
# configuration file /etc/nginx/conf.d/default.conf:
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#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.conf 文件被完全忽略了。就像
"-c", "/etc/nginx/nginx.conf"
从 Dockerfile 中的 cmd 永远不会 运行s。 我试图在 docker 运行 中传递命令,但没有成功。
你能帮帮我吗?
经过大量的反复试验,我终于成功完成了这项工作。首先将 Dockerfile 中的图像从:nginxinc/nginx-unprivileged 更改为 nginx:alpine
其次,给openshift里面的用户适当的权限。 运行 :
oc adm policy add-scc-to-user anyuid system:serviceaccount:<<NAMESPACE>>:default
接下来应在项目的根目录中创建一个 default.conf 文件,并且在服务器 {} 对象中具有与自定义 nginx.conf 文件相同的内容。最后应该对所有文件进行正确的配置。
default.conf:
server { # simple reverse-proxy
root /usr/share/nginx/html;
listen 80;
# pass requests for dynamic content to rails/turbogears/zope, et al
location /test1/ {
proxy_pass http://dumy/test1/;
}
location /test2/ {
proxy_pass http://dumy/test2/;
}
}
nginx.conf:
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
#include /etc/nginx/sites-enabled/*;
server { # simple reverse-proxy
root /var/www/html;
listen 80;
# pass requests for dynamic content to rails/turbogears/zope, et al
location /test1/ {
proxy_pass http://dumy/test1/;
}
location /test2/ {
proxy_pass http://dumy/test2/;
}
}
}
Docker 文件:
FROM node:alpine AS builder
WORKDIR '/app'
COPY package.json .
RUN npm install
COPY . .
RUN npm run prod
FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/
COPY default.conf /etc/nginx/conf.d/
RUN chgrp -R root /var/cache/nginx /var/run /var/log/nginx && \
chmod -R 770 /var/cache/nginx /var/run /var/log/nginx
EXPOSE 8080
CMD ["nginx","-g", "daemon off;"]
请记住,在 default.conf 和 nginx.conf 文件中,应更改服务器对象内的根目录以匹配静态文件 html 文件的路径。在我的例子中 docker 内的路径是:/usr/share/nginx/html 所以整个根变量应该是:
root /usr/share/nginx/html;