【Nginx】Docker中部署反向代理

本文介绍了如何在服务器 Centos7 上使用 Docker 搭建 Nginx 实现反向代理。

  1. 安装 Docker

  2. 配置 Docker 数据保存路径

  3. 设置 Docker 代理

  4. 预先拉取镜像

    1
    docker pull nginx
  5. 创建配置文件

    1
    2
    3
    mkdir -p /root/nginx_proxy
    cd /root/nginx_proxy
    vim nginx.conf

    以下是配置文件(暂时不太正确)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    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;

    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;
    #}
    }
    }
  6. 部署 Nginx

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    docker run -d \
    --name nginx_proxy \
    --restart always \
    --link nextcloud \
    --link gitlab \
    -p 80:80 \
    -p 443:443 \
    -v /root/nginx_proxy/nginx.conf:/etc/nginx/nginx.conf \
    -v /root/nginx_proxy/ssl:/root/ssl/ \
    -v /home/git/www:/usr/share/nginx/html/ \
    nginx

【Nginx】Docker中部署反向代理

https://biteax.com/a2eef2c1.html

作者

石志超

发布于

2022-01-26

更新于

2023-09-27

许可协议