我的本地机器上有 nginx,它重定向所有 http 流量端口 80 至 8008通过这个简单的配置:
server {
listen 8008;
# location some-location {
#
# }
location / {
proxy_pass http://localhost:80/index.php/;
proxy_set_header X-Real-IP $remote_addr;
}
}
本地一切正常,但我想将 nginx 放置在 docker 容器中。我的 Dockerfile:
FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 8008 80
但是当我用 cmd 运行我的图像时
docker run -d -p 8008:80 <image ID>
什么也没发生,容器停止了。日志在这里:
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2f2602efc455 a58e81b31db6 "nginx -g 'daemon of…" 10 seconds ago Exited (1) 10 seconds ago unruffled_galileo
docker logs
2018/01/03 12:17:08 [emerg] 1#1: "server" directive is not allowed here in /etc/nginx/nginx.conf:1 nginx: [emerg] "server" directive is not allowed here in /etc/nginx/nginx.conf:1
我究竟做错了什么?
答案1
好的,终于在我的nginx.conf中发现了错误:user nginx;工作进程 1;
error_log /var/log/nginx/error.log warn;
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;
keepalive_timeout 65;
server {
location / {
proxy_pass http://localhost:80/index.php/;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
Dockerfile:
FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 8008
并使用命令启动图像:
docker run -d -p 8008:80 <ID>