【代码片段】使用docker部署nginx 并通过nginx设置密码访问控制

使用docker部署nginx服务

docker-compose.yml

version: '3'

services:
  web:
    image: nginx
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
    restart: always
    ports:
      - "80:80"
      - "443:443"
    environment:
      - NGINX_HOST=fengleer.cn
      - NGINX_PORT=80

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;

    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen 80 default_server;
        server_name jupyter.my.local;
        client_max_body_size 1G;
        location / {
            proxy_pass http://172.17.0.1:8086;
            proxy_connect_timeout 3s;
            proxy_read_timeout 5s;
            proxy_send_timeout 3s;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_redirect off;
        }
    }
}


配置密码

安装依赖

sudo apt-get install apache2-utils

生成用户名,密码对

#创建新的密码文件
htpasswd -c ./ht.passwd test

#向密码文件中追加用户
htpasswd -c ./ht.passwd test1

将密码文件挂载到docker中

volumes:
  - ./nginx.conf:/etc/nginx/nginx.conf
  - ./ht.passwd:/etc/nginx/ht.passwd

针对对应的服务添加用户访问控制

    server {
        # ----
        location / {
            # ---
            auth_basic "Please enter your username and password";
            auth_basic_user_file /etc/nginx/htpasswd;
            autoindex on;
        }
    }
  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用 Docker 部署 Nginx使用 Nginx 部署项目的步骤: 1. 首先,需要安装 Docker,可以参考官网的安装教程进行安装。 2. 创建一个用于存放 Nginx 配置文件和项目文件的目录,比如 /home/nginx。 3. 在该目录下创建一个名为 nginx.conf 的 Nginx 配置文件,可以参考以下示例进行编写: ``` worker_processes 1; events { worker_connections 1024; } http { server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html; } location /api/ { proxy_pass http://backend:8000/; } } } ``` 上述配置文件中,Nginx 监听本地 80 端口,将访问根目录的请求转发到 /usr/share/nginx/html 目录下的 index.html 文件,将以 /api/ 开头的请求转发到名为 backend 的 Docker 容器中的 8000 端口。 4. 在该目录下创建一个名为 Dockerfile 的文件,用于构建自定义的 Nginx 镜像,可以参考以下示例进行编写: ``` FROM nginx COPY nginx.conf /etc/nginx/nginx.conf COPY dist/ /usr/share/nginx/html/ ``` 上述 Dockerfile 文件中,使用 nginx 作为基础镜像,将本地的 nginx.conf 文件复制到容器中的 /etc/nginx/nginx.conf 目录下,将本地的项目文件复制到容器中的 /usr/share/nginx/html/ 目录下。 5. 使用以下命令构建并运行 Nginx 容器: ``` docker build -t my-nginx . docker run --name my-nginx -p 80:80 --link backend my-nginx ``` 上述命令中,首先使用 docker build 命令构建名为 my-nginx 的自定义 Nginx 镜像,然后使用 docker run 命令启动该镜像对应的容器,将本地的 80 端口映射到容器的 80 端口,并连接名为 backend 的后端容器。 6. 至此,使用 Docker 部署 Nginx使用 Nginx 部署项目的过程已完成。可以通过浏览器访问 http://localhost 来查看部署的项目。如果该项目中有需要通过 /api/ 访问的后端服务,可以在后端容器中启动该服务,并将其命名为 backend,以便 Nginx 容器能够连接到该服务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值