Docker 创建Nginx并自启动

Docker创建最新版本

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;
        # 指定前端项目所在的位置
        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;
        # 指定前端项目所在的位置
        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;
        }
    }
}

已经创建好内部网络heima

docker network create heima
docker network ls

启动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;
        # 指定前端项目所在的位置
        location / {
            root html;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       18080;
        server_name  localhost;
        # 指定前端项目所在的位置
        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;
        # 指定前端项目所在的位置
        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;
        # 指定前端项目所在的位置
        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;
    # }  
}

启动查看不到页面, 查看原因

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容