Docker 部署 Nginx

2023-11-11  本文已影响0人  tzktzk1

案例:需求

在Docker容器中部署Nginx,并通过外部机器访问Nginx。

案例:实现步骤

1.搜索Nginx镜像 docker search nginx
2.拉取Nginx镜像 docker pull nginx
3.创建容器

在/home目录下创建nginx目录用于存储nginx数据信息

mkdir /home/nginx
cd /home/nginx
mkdir conf
cd conf

在/home/nginx/conf/下创建nginx.conf文件,粘贴下面内容

vim nginx.conf

# 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;
}

在/home/nginx/conf/conf.d/下创建default.conf文件,粘贴下面内容

vim default.conf

# default.conf文件内容
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

linux下部署命令:

docker run -itd --name c_nginx -p 8000:80 -v /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /home/nginx/conf/conf.d/default.conf:/etc/nginx/conf.d/default.conf -v /home/nginx/logs:/var/log/nginx -v /home/nginx/html:/usr/share/nginx/html nginx:latest

windows下部署命令:

docker run -itd --name c_nginx -p 8000:80 -v D:/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v D:/docker/nginx/conf/conf.d/default.conf:/etc/nginx/conf.d/default.conf -v D:/docker/nginx/logs:/var/log/nginx -v D:/docker/nginx/html:/usr/share/nginx/html nginx:latest

参数说明:

4.测试访问

使用外部机器访问nginx: 宿主机IP
第一次访问会什么都没有,是因为/html目录下什么都没放,可以vim index.html建立一个页面文件
编辑内容:<h1>hello nginx docker </h1> :wq 保存退出,刷新浏览器页面就有欢迎页了。

上一篇 下一篇

猜你喜欢

热点阅读