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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM