docker安装nginx

一 拉取nginx镜像

docker pull nginx

不写版本的话默认是最新版本

二.创建文件挂载目录

mkdir -p /xj/nginx/{conf,conf.d,html,logs}

三.创建配置文件

1.在 /xj/nginx/conf/下创建配置文件 nginx.conf,然后将一下内容复制进去

user  root;
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;
}

2.在/xj/nginx/conf.d目录下创建 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;
    #}
}

说明:

1.第一个配置文件里的第一行是用户配置,需要和启动用户一致,我的服务器是root用户,这里就用root,如果不一直可能会出现403的问题;

2.nginx是先加载一个主配置文件nginx.conf,在nginx.conf里再加载conf.d目录下的子配置文件(一般最少一个default.conf文件),所以我们一般将需要的配置信息写在default.conf文件中.

四.运行nginx容器

docker run --name nginx -d -p 90:80 -v /xj/nginx/html:/usr/share/nginx/html -v /xj/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro -v /xj/nginx/conf.d:/etc/nginx/conf.d nginx:1.23

五.验证是否配置成功

在/xj/nginx/html目录下新建一个index.html文件,内容为 hello nginx,然后访问 服务器地址:端口

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Docker安装Nginx,可以按照以下步骤进行操作: 1. 首先,查看可用的Nginx版本。可以访问Nginx镜像库地址https://hub.docker.com/_/nginx?tab=tags来获取可用的版本信息。 2. 下载Nginx镜像。可以使用命令`docker pull nginx`来下载最新版的Nginx镜像。也可以使用`docker pull nginx:xxx`来下载指定版本的Nginx镜像,其中`xxx`是具体的版本号。 3. 创建Nginx容器。可以使用以下命令来创建Nginx容器: ``` docker run \ -p 9002:80 \ --name nginx \ -v /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \ -v /home/nginx/conf/conf.d:/etc/nginx/conf.d \ -v /home/nginx/log:/var/log/nginx \ -v /home/nginx/html:/usr/share/nginx/html \ -d nginx:latest ``` 这个命令会启动一个名为`nginx`的容器,将容器的9002端口映射到主机的80端口,同时挂载了配置文件、日志文件和内容文件的目录。 这样,你就成功安装Docker中的Nginx。你可以通过访问`http://localhost:9002`来验证Nginx是否正常运行。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Docker 安装 Nginx](https://download.csdn.net/download/weixin_38698863/14893474)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Docker 安装 Nginx 容器 (完整详细版)](https://blog.csdn.net/BThinker/article/details/123507820)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值