我是Nginx的新手,我在一个码头容器中运行Nginx来服务一个简单的网站。我想添加一个简单返回状态200 +一些任意内容的/health端点。
我复制并调整了标准nginx.conf从/etc/nginx/通过添加
server {
location /health {
return 200 "alive";
}
}在http块的底部。但是当我运行Docker并尝试访问localhost/health时,我只得到了no such file or directory。访问localhost的网站很好。
我还尝试复制其他代码块,例如,这个代码块:https://gist.github.com/dhrrgn/8650077,但之后我得到了conflicting server name "" on 0.0.0.0:80, ignored nginx: [warn] conflicting server name "" on 0.0.0.0:80, ignored。
我是不是把location放在了nginx.conf里面的错误位置?我需要一些特殊的服务器配置吗?有什么问题吗?
发布于 2018-05-15 08:17:12
问题在于我的Nginx安装/配置:我使用的是nginx:alpine,它在/etc/nginx/conf.d/上有配置文件。在那里,default.conf定义了Nginx的默认配置。因此,我不得不删除default.conf并将配置复制到那里。在Dockerfile中
COPY nginx.conf /etc/nginx/conf.d/nginx.conf
RUN rm /etc/nginx/conf.d/default.conf当然,我还必须在nginx.conf中定义标准路由:
server {
location / {
root /usr/share/nginx/html;
}
location /health {
return 200 'alive';
add_header Content-Type text/plain;
}
}发布于 2020-12-18 17:07:12
如果您想在不构建图像的情况下在一行中执行此操作,则只需执行以下操作:
#1创建Nginx.conf文件
nano /tmp/nginx-tester/nginx.conf并将以下内容放在那里:
events {}
http {
server {
location / {
root /usr/share/nginx/html;
}
location /health {
return 200 '{"status":"UP"}';
add_header Content-Type application/json;
}
}
}如果您看到,它所做的一切就是提供一个http状态200,其中一个json声明状态是UP
#2运行NgInx图像
只需将其放在一行中,并避免每次重新创建图像,只需指定如下所示的卷:
docker run -it --rm -d -p 8077:80 --name nginx-tester -v /tmp/nginx-tester/nginx.conf:/etc/nginx/nginx.conf:ro nginx-it:交互式进程(如shell)--rm:容器退出时被移除-p 8077:80:主机端口:容器端口--name:容器的名称-v:绑定装入卷(文件主机:文件容器:ReadOnly)nginx:将下载并运行的图像#3测试it
您只需转到server8077 (这是您在步骤#2__中指定的端口)就可以做到这一点。
~ > curl http://myserver:8077/health
{"status":"UP"}通过这种方式,您可以更改配置文件,只需执行以下操作:
docker restart nginx-tester您可以重新加载文件,而不需要重建图像。
https://stackoverflow.com/questions/50295614
复制相似问题