欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

docker啟動nginx無法訪問的問題解決

 更新時間:2025年02月19日 10:41:59   作者:Dedete_  
本文主要介紹了docker啟動nginx無法訪問的問題解決,主要遇到404錯誤,問題原因是配置文件路徑錯誤和權(quán)限問題,下面就來具體介紹一下問題解決,感興趣的可以了解一下

項目場景:

項目相關(guān)背景:在學習黑馬springcloud課程的多級緩存時,使用docker啟動nginx容器

問題描述

項目中遇到的問題:依此使用下列命令部署nginx容器

docker pull nginx
docker volume create nx_conf #新建nginx的nginx.conf配置掛載的數(shù)據(jù)卷
docker volume create nx_html #新建ngxin的html靜態(tài)資源掛載的數(shù)據(jù)卷
docker run --name nx \
-p xxxx:80 \
-v nx_conf:/etc/nginx \
-v nx_html:/usr/share/nginx/html \
-d nginx

使用docker volume inspect nx_conf查看數(shù)據(jù)卷位置

nx_conf數(shù)據(jù)卷目錄下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;
}

老師發(fā)的nginx通用配置文件nginx.conf如下

#user  nobody;
worker_processes  1;
error_log  logs/error.log;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

因為配置了其他訪問路徑,我把include /etc/nginx/conf.d/*.conf;注釋掉,并把老師配置文件中location指令中關(guān)于/=/50x.html命令復制到自己的配置文件中
訪問結(jié)果如下圖所示:

在這里插入圖片描述

原因分析:

問題的分析:

結(jié)果表示nginx服務(wù)已經(jīng)開啟,但出現(xiàn)404錯誤,404錯誤是請求的網(wǎng)頁不存在,查看路徑是否配置正確

解決方案:

具體解決方案:

  • 解決方案1
    取消include /etc/nginx/conf.d/*.conf;注釋(啟用默認配置),在默認配置nx_conf/nginx.conf的default.conf文件中編輯添加自己的訪問路徑
  • 解決方案2
    location指令root html改成root /usr/share/nginx/html;
  • 解決方案3(推薦)
    使用docker run命令掛載數(shù)據(jù)卷時使用-v nx_conf:/etc/nginx/conf.d掛載默認文件目錄

總結(jié)

本人對nginx文件目錄不熟悉導致的404錯誤,記錄此次遇到問題,找出原因,解決方案的步驟以加強本人對此問題的印象。

到此這篇關(guān)于docker啟動nginx無法訪問的問題解決的文章就介紹到這了,更多相關(guān)docker啟動nginx無法訪問內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論