【Docker】Nginx 容器化部署

轻云NGINX托管静态网站模块

轻云NGINX 默认挂载了/app的卷。你可以把宿主机上的文件夹挂载到该目录下。

你可以通过轻云UC部署工具直接安装部署,也可以手动按如下文档操作,该项目已经全面开源,可以从如下环境获取

开源地址: https://gitee.com/qingplus/qingcloud-platform

Qinghub Studio 在线体验

容器示例

在示例中,我们将创建一个 nginx 客实例。

适用docker 命令行

创建 nginx服务器实例
docker run -v /path/to/app:/app nginx:latest

或者通过修改docker-compose.yml文件:

services:
  nginx:
  ...
    volumes:
      - /path/to/app:/app
  ...

从主机访问您的服务器

要从主机访问 Web 服务器,您可以要求 Docker 将主机上的随机端口映射到9000端口。

docker run --name nginx -P nginx:latest

运行docker port以确定 Docker 分配的随机端口。

$ docker port nginx
9000/tcp -> 0.0.0.0:32769

您还可以手动指定要从主机转发到容器的端口。

docker run -p 9000:9000 nginx:latest

通过http://localhost:9000来访问您的浏览器中的 Web 服务器。

配置

添加自定义服务器块

默认情况下nginx.conf包括放置在/etc/nginx/nginx.conf。他会包含如下目录/etc/nginx/conf.d中的所有.conf文件,您可以my_server_block.conf在此位置挂载包含自定义服务器块的文件。

例如,为了添加一个服务器块www.example.com:

第 1 步:使用以下内容编写文件my_server_block.conf

server {
  listen 0.0.0.0:9100;
  server_name www.example.com;
  root /app;
  index index.htm index.html;
}

第 2 步:将配置挂载为卷

docker run --name nginx \
  -v /path/to/my_server_block.conf:/etc/nginx/conf.d/my_server_block.conf:ro \
  nginx:latest

或者通过修改docker-compose.yml文件:

services:
  nginx:
  ...
    volumes:
      - /path/to/my_server_block.conf:/etc/nginx/conf.d/my_server_block.conf:ro
  ...

使用自定义 SSL 证书

注意:以下步骤假设您使用自定义域名,并且您已将自定义域名配置为指向您的服务器。

第 1 步:准备您的证书文件

在您的本地计算机中,创建一个名为 的文件夹certs并放置您的证书文件。确保将两个文件分别重命名为server.crt和server.key:

mkdir -p /path/to/nginx-persistence/certs
cp /path/to/certfile.crt /path/to/nginx-persistence/certs/server.crt
cp /path/to/keyfile.key  /path/to/nginx-persistence/certs/server.key
第 2 步:为 SSL 连接提供自定义服务器块

my_server_block.conf使用 SSL 配置和证书的相对路径写入文件:

  server {
    listen       8443 ssl;

    ssl_certificate      certs/server.crt;
    ssl_certificate_key  certs/server.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;

    location / {
      root   html;
      index  index.html index.htm;
    }
  }
第 3 步:运行NGINX开源镜像并打开SSL端口

运行 NGINX image,从主机挂载证书目录。

docker run --name nginx \
  -v /path/to/my_server_block.conf:/etc/nginx/conf.d/my_server_block.conf:ro \
  -v /path/to/nginx-persistence/certs:/etc/nginx/certs \
  nginx:latest

或者通过修改docker-compose.yml文件:

services:
  nginx:
  ...
    volumes:
    - /path/to/nginx-persistence/certs:/etc/nginx/certs
    - /path/to/my_server_block.conf:/etc/nginx/conf.d/my_server_block.conf:ro
  ...

解决重定向问题

默认情况下,NGINX image发出的重定向是相对的。如果您需要激活绝对重定向,您可以设置NGINX_ENABLE_ABSOLUTE_REDIRECT为yes。您应该注意容器正在侦听的端口,否则它不会出现在重定向除非NGINX_ENABLE_PORT_IN_REDIRECT也设置为yes。

在下面几行中,您可以看到解释重定向如何工作的不同示例。所有这些都假设我们在服务器块中有以下内容my_redirect_server_block.conf:

server {
  listen 0.0.0.0:9000;
  server_name www.example.com;
  root /app;
  index index.htm index.html;
  location /test/ {
    return 301 /index.html;
  }
}
默认配置
docker run --name nginx --rm -p 9000:9000 \
  -v /path/to/my_redirect_server_block.conf:/etc/nginx/conf.d/my_redirect.conf:ro \
  nginx:latest

如前所述,NGINX 镜像发出的默认重定向是相对的。客户端应构建最终 URL

$ curl -kI http://localhost:9000/test/
HTTP/1.1 301 Moved Permanently
...
Location: /index.html
...
$ curl -w %{redirect_url}\\n -o /dev/null http://localhost:9000/test/
http://localhost:9000/index.html

请记住,某些旧客户端可能与相对重定向不兼容。

启用绝对重定向
docker run --name nginx --rm -p 9000:8080 \
  -v /path/to/my_redirect_server_block.conf:/etc/nginx/conf.d/my_redirect.conf:ro \
  -e NGINX_ENABLE_ABSOLUTE_REDIRECT=yes \
  nginx:latest

因此,容器将在Location标头中回复完整的 URL,但没有端口。如果您在标准端口(80 或 443)中公开容器,这非常有用

$ curl -kI http://localhost:9000/test/
HTTP/1.1 301 Moved Permanently
...
Location: http://localhost/index.html
...
重定向中的端口已启用
docker run --name nginx --rm -p 9000:8080 \
  -v /path/to/my_redirect_server_block.conf:/etc/nginx/conf.d/my_redirect.conf:ro \
  -e NGINX_ENABLE_ABSOLUTE_REDIRECT=yes \
  -e NGINX_ENABLE_PORT_IN_REDIRECT=yes \
  nginx:latest

在这种情况下,容器将包含它在重定向中侦听的端口,而不是它公开的端口(在示例8080vs9000)

$ curl -kI http://localhost:9000/test/
HTTP/1.1 301 Moved Permanently
...
Location: http://localhost:8080/index.html
...

要修改这种情况并构建可访问的 URL,您必须在您公开的同一端口中运行侦听容器

docker run --name nginx --rm -p 9000:9000 \
  -v /path/to/my_redirect_server_block.conf:/etc/nginx/conf.d/my_redirect.conf:ro \
  -e NGINX_ENABLE_ABSOLUTE_REDIRECT=yes \
  -e NGINX_ENABLE_PORT_IN_REDIRECT=yes \
  -e NGINX_HTTP_PORT_NUMBER=9000
  nginx:latest

配置

该镜像配置在/etc/nginx/nginx.conf。您可以nginx.conf使用自己的自定义配置文件覆盖该文件。

docker run --name nginx \
  -v /path/to/your_nginx.conf:/etc/nginx/nginx.conf:ro \
  nginx:latest

或者通过修改docker-compose.yml此存储库中存在的文件:

services:
  nginx:
  ...
    volumes:
      - /path/to/your_nginx.conf:/etc/nginx/nginx.conf:ro
  ...

反向代理到其他容器

NGINX 可用于使用 Docker 的链接系统反向代理到其他容器。如果您想通过 NGINX 前端提供动态内容,这尤其有用。为此,请在文件夹中添加如下所示的服务器块/etc/nginx/conf.d/:

server {
    listen 0.0.0.0:8080;
    server_name yourapp.com;
    access_log /etc/nginx/logs/yourapp_access.log;
    error_log /etc/nginx/logs/yourapp_error.log;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header HOST $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://[your_container_alias]:[your_container_port];
        proxy_redirect off;
    }
}

参考:

日志

docker logs nginx

或使用 Docker Compose:

docker-compose logs nginx
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值