Docker 創(chuàng)建Nginx并自啟動(dòng)

Docker創(chuàng)建最新版本

docker run -itd \
--name nginx \
--restart=always \
-p 80:80 \
-v /mydata/nginx_decai/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /mydata/nginx_decai/conf/conf.d:/etc/nginx/conf.d \
-v /mydata/nginx_decai/log:/var/log/nginx \
-v /mydata/nginx_decai/html:/usr/share/nginx/html \
-d nginx:latest

nginx.conf 參考配置


worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/json;

    sendfile        on;
    
    keepalive_timeout  65;

    server {
        listen       18080;
        # 指定前端項(xiàng)目所在的位置
        location / {
            root /usr/share/nginx/html/hmall-portal;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location /api {
            rewrite /api/(.*)  /$1 break;
            proxy_pass http://hmall:8080;
        }
    }
    server {
        listen       18081;
        # 指定前端項(xiàng)目所在的位置
        location / {
            root /usr/share/nginx/html/hmall-admin;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location /api {
            rewrite /api/(.*)  /$1 break;
            proxy_pass http://hmall:8080;
        }
    }
}

已經(jīng)創(chuàng)建好內(nèi)部網(wǎng)絡(luò)heima

docker network create heima
docker network ls

啟動(dòng)nginx

docker run -d \
--name nginx \
-p 18080:18080 \
-p 18081:18081 \
--restart=always \
-v /Users/leo/Documents/Workspace/docker_data/nginx/html:/usr/share/nginx/html \
-v /Users/leo/Documents/Workspace/docker_data/nginx/nginx.conf:/etc/nginx/nginx.conf \
--network heima \
nginx

nginx.conf參考2


worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/json;

    sendfile        on;
    
    keepalive_timeout  65;

     server {
        listen       80;
        server_name  localhost;
        # 指定前端項(xiàng)目所在的位置
        location / {
            root html;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       18080;
        server_name  localhost;
        # 指定前端項(xiàng)目所在的位置
        location / {
            root html/hmall-portal;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location /api {
            rewrite /api/(.*)  /$1 break;
            proxy_pass http://localhost:8080;
        }
    }
    server {
        listen       18081;
        server_name  localhost;
        # 指定前端項(xiàng)目所在的位置
        location / {
            root html/hmall-admin;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location /api {
            rewrite /api/(.*)  /$1 break;
            proxy_pass http://localhost:8080;
        }
    }
    server {
        listen       18082;
        server_name  localhost;
        # 指定前端項(xiàng)目所在的位置
        location / {
            root html/hm-refresh-admin;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location /api {
             proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Real-PORT $remote_port;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            rewrite /api/(.*)  /$1 break;
            proxy_pass http://localhost:8080;
        }
    }
    # upstream backend {
    #     server 127.0.0.1:8081 max_fails=5 fail_timeout=10s weight=1;
    #     server 127.0.0.1:8082 max_fails=5 fail_timeout=10s weight=1;
    # }  
}

啟動(dòng)查看不到頁(yè)面, 查看原因

docker logs -f nginx
image.png

把他添加到/etc/nginx/html就能看到

docker run -d \
--name nginx \
-p 18080:18080 \
-p 18081:18081 \
--restart=always \
-v /Users/leo/Documents/Workspace/docker_data/nginx/html:/usr/share/nginx/html \
-v /Users/leo/Documents/Workspace/docker_data/nginx/html:/etc/nginx/html \
-v /Users/leo/Documents/Workspace/docker_data/nginx/nginx.conf:/etc/nginx/nginx.conf \
nginx:latest
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容