Nginx with Docker : nginx: [emerg] unknown directive "enable" in /etc/nginx/nginx.conf
Nginx with Docker : nginx: [emerg] unknown directive "enable" in /etc/nginx/nginx.conf
我有两台服务器,一台在数字海洋上,另一台在其他地方。我有一个项目,它与该服务器上的所有应用程序配合良好。但是,我想创建一个 droplet 并将其移动到数字海洋,所以我通过 sudo scp
命令将所有文件复制到新服务器(数字海洋)。除了 Nginx 返回退出代码 1 之外,所有服务都运行良好,如下图所示。整个代码都是一样的,所以我很困惑为什么它不能像另一个一样工作。
我可以知道我可能遗漏了什么吗?
这是我的 nginx.conf 文件 :
worker_processes 4;
events {
worker_connections 4096;
}
http {
server {
listen 80 default_server;
server_name "";
return 444;
}
server {
server_name game-dev.myappapp.com;
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_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://game_nodes;
proxy_redirect off;
}
}
server {
if ($host = game-dev.myappapp.com) {
return 301 https://$host$request_uri;
}
listen 80;
listen [::]:80;
server_name game-dev.myappapp.com;
return 404;
}
upstream game_nodes {
# enable sticky session
#ip_hash;
server game-alpha:3000;
keepalive 8;
}
server {
server_name api-dev.myappapp.com;
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_pass http://main_nodes;
proxy_redirect off;
}
}
server {
if ($host = api-dev.myappapp.com) {
return 301 https://$host$request_uri;
}
listen 80;
listen [::]:80;
server_name api-dev.myappapp.com;
return 404;
}
upstream main_nodes {
server main-alpha:8000;
server main-beta:8000;
keepalive 8;
}
}
这是我的 Dockerfile :
FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
正如你从下面的截图中看到的(左边的服务器是我的 droplet,右边的是我的另一台服务器)我的 droplet 没有正常监听 80 端口它应该监听另一台服务器的 80 端口。
这是我来自 DigitalOcean Droplet 的 Nginx 日志。
root@knowin-project-dev:/home/cihat/app# docker logs nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/06/13 20:22:46 [emerg] 1#1: unknown directive "enable" in /etc/nginx/nginx.conf:45
nginx: [emerg] unknown directive "enable" in /etc/nginx/nginx.conf:45
是nginx.co.conf来自DigitalOcean droplet:
root@knowin-project-dev:/home/cihat/app# docker run -it nginx /bin/bash
root@1c54013ff2c8:/# cat etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
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;
}
注意:ufw 的状态为未激活。所以所有端口都可用。
阅读错误信息:
2021/06/13 20:22:46 [emerg] 1#1: unknown directive "enable" in /etc/nginx/nginx.conf:45
nginx: [emerg] unknown directive "enable" in /etc/nginx/nginx.conf:45
您的配置文件中存在语法错误 #45
您共享的配置文件在第 45 行看起来不错,因为您的“启用粘性会话”已用 #
符号注释掉。
通过输入
确保运行容器中的配置文件是相同的
docker exec nginx /bin/bash -c "cat /etc/nginx/nginx.conf" | tee nginx.co.conf
在您的数字海洋服务器上。请分享 nginx.co.conf
文件的输出,并确保第 45 行也被注释掉。
您是如何部署 docker-容器的?您是否将它们导出为 tar 并上传到新服务器?听起来你的本地容器和数字海洋上的容器有些不同。
我有两台服务器,一台在数字海洋上,另一台在其他地方。我有一个项目,它与该服务器上的所有应用程序配合良好。但是,我想创建一个 droplet 并将其移动到数字海洋,所以我通过 sudo scp
命令将所有文件复制到新服务器(数字海洋)。除了 Nginx 返回退出代码 1 之外,所有服务都运行良好,如下图所示。整个代码都是一样的,所以我很困惑为什么它不能像另一个一样工作。
我可以知道我可能遗漏了什么吗?
这是我的 nginx.conf 文件 :
worker_processes 4;
events {
worker_connections 4096;
}
http {
server {
listen 80 default_server;
server_name "";
return 444;
}
server {
server_name game-dev.myappapp.com;
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_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://game_nodes;
proxy_redirect off;
}
}
server {
if ($host = game-dev.myappapp.com) {
return 301 https://$host$request_uri;
}
listen 80;
listen [::]:80;
server_name game-dev.myappapp.com;
return 404;
}
upstream game_nodes {
# enable sticky session
#ip_hash;
server game-alpha:3000;
keepalive 8;
}
server {
server_name api-dev.myappapp.com;
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_pass http://main_nodes;
proxy_redirect off;
}
}
server {
if ($host = api-dev.myappapp.com) {
return 301 https://$host$request_uri;
}
listen 80;
listen [::]:80;
server_name api-dev.myappapp.com;
return 404;
}
upstream main_nodes {
server main-alpha:8000;
server main-beta:8000;
keepalive 8;
}
}
这是我的 Dockerfile :
FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
正如你从下面的截图中看到的(左边的服务器是我的 droplet,右边的是我的另一台服务器)我的 droplet 没有正常监听 80 端口它应该监听另一台服务器的 80 端口。
这是我来自 DigitalOcean Droplet 的 Nginx 日志。
root@knowin-project-dev:/home/cihat/app# docker logs nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/06/13 20:22:46 [emerg] 1#1: unknown directive "enable" in /etc/nginx/nginx.conf:45
nginx: [emerg] unknown directive "enable" in /etc/nginx/nginx.conf:45
是nginx.co.conf来自DigitalOcean droplet:
root@knowin-project-dev:/home/cihat/app# docker run -it nginx /bin/bash
root@1c54013ff2c8:/# cat etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
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;
}
注意:ufw 的状态为未激活。所以所有端口都可用。
阅读错误信息:
2021/06/13 20:22:46 [emerg] 1#1: unknown directive "enable" in /etc/nginx/nginx.conf:45
nginx: [emerg] unknown directive "enable" in /etc/nginx/nginx.conf:45
您的配置文件中存在语法错误 #45
您共享的配置文件在第 45 行看起来不错,因为您的“启用粘性会话”已用 #
符号注释掉。
通过输入
确保运行容器中的配置文件是相同的docker exec nginx /bin/bash -c "cat /etc/nginx/nginx.conf" | tee nginx.co.conf
在您的数字海洋服务器上。请分享 nginx.co.conf
文件的输出,并确保第 45 行也被注释掉。
您是如何部署 docker-容器的?您是否将它们导出为 tar 并上传到新服务器?听起来你的本地容器和数字海洋上的容器有些不同。