docker nginx 运行后无法访问

## 1

最近在学docker部署,一开始打算将nginx先docker化的。
对照官方的docker镜像介绍说明,进行自定义配置

将官方的nginx.conf复制出来后,修改添加了一些自定义,主要是屏蔽了default.conf,以及include文件夹 sites-available

# include /etc/nginx/conf.d/.conf;
include /etc/nginx/sites-available/
;

官方原先配置

user  nginx;
worker_processes  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;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

新建docker-compose.yml 简单的 指定images,名字,端口,挂载本地文件替代默认

version: '3'
services:
  nginx-proxy:
    image: nginx
    container_name: nginx
    ports:
      - 8081:80
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro

## 2

运行docker-compose up 后,一直卡在attaching to nginx,浏览器也是无法访问该端口地址

Starting nginx ... done
Attaching to nginx

不知道问题出在哪里,查找资料后发现可以使用tty参数进行调试。
修改docker-compose.yml,增加一个配置tty:true。

docker exec -it nginx /bin/bash

发现自己把默认的default.conf删除后,没有添加其他的配置文件,之前的sites-available文件夹是空的。

## 3

自己把自己坑了,添加

-./nginx/sites-available:/etc/nginx/sites-available:ro

并在sites-available添加一个配置文件。

/etc/nginx/sites-available# ls
default.conf

运行后,对端口地址访问终于正常了
cmd-markdown-logo

猜你喜欢

转载自www.cnblogs.com/duoxuan/p/9263633.html