docker安装niginx

docker 安装 Nginx

准备工作

# 打开目录

[root@localhost \]# cd /usr/local/docker

# 创建文件夹

[root@localhost docker]# mkdir nginx

# 进入文件夹

[root@localhost docker]# cd nginx/

# 创建 config

[root@localhost nginx]# mkdir conf

# 打开 config

[root@localhost nginx]# cd conf

# 编写配置文件

[root@localhost conf]# vim 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;

}

下载镜像

[root@localhost conf]# docker pull nginx:latest

编写脚本

# 打开目录

[root@localhost conf]# cd /usr/local/docker/nginx

# 编写脚本

[root@localhost nginx]# vim startNginx.sh

脚本内容

docker run -d \

 --restart=always \

 --name nginx \

 -p 80:80 \

 -v /usr/local/docker/nginx/conf/conf.d:/etc/nginx/conf.d \

 -v /usr/local/docker/nginx/html:/usr/share/nginx/html \

nginx

启动脚本

# 赋予权限

[root@localhost nginx]# chmod -R 777 startNginx.sh

# 启动脚本

[root@localhost nginx]# ./startNginx.sh

在conf.d目录新增:static-gmall.conf文件

# 打开目录

[root@localhost nginx]# cd /usr/local/docker/nginx/conf/conf.d

# 编写static-gmall.conf文件

[root@localhost conf.d]# vim static-gmall.conf

文件内容

server {

    listen       80;

    server_name  static.gmall.com;

    location /static/ {

        root   /usr/share/nginx/html;

    }

如果想要转发网关

server {

    listen       80;
    server_name  jiuzhou.com;

        location / {
            root /usr/share/nginx/html;
            try_files $uri $uri/ @router;
            index /index.html;
        }

        location @router {
          rewrite ^.*$ /index.html last;
        }

        location /api/ {
          proxy_set_header Host $http_host;
          proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header REMOTE-HOST $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://172.17.0.1:9999/;
        }
}
172.17.0.1这个地址是你容器的网关ip,相当于桥梁。(重要)

root /usr/share/nginx/html;这个是容器内部的地址(重要)

}

重启脚本

# 重启脚本

[root@localhost conf.d]# docker restart 容器ID

更改windows中host设置

# 打开 本机此电脑 输入

C:\Windows\System32\drivers\etc

# 进入指定文件夹

# 打开名为 host 文件

# 拉到最下面 添加

你虚拟机地址 域名地址 例:joakim.com

# 添加后进行保存

# 正常使用域名访问图片

http://joakim.com/static/1677120963562.png

基础命令

虚拟机防火墙命令

查询防火墙状态,绿色的时开启了防火墙,黑色的是表示防火墙关闭

  systemctl status firewalld

关闭防火墙

  systemctl stop firewalld

开启防火墙

  systemctl start firewalld

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值