Nginx使用说明

一、需求说明

我有一个linux服务器,服务器上启动了docker,我有两个docker容器分别是两个web服务,分别是xx和yy:

# docker ps
CONTAINER ID        IMAGE             COMMAND                  CREATED             STATUS              PORTS                     NAMES
94d0294286f8        xx                "catalina.sh run"        19 hours ago        Up 19 hours         0.0.0.0:8088->8088/tcp    xx
359d06deb64c        yy                "catalina.sh run"        12 days ago         Up 12 days          0.0.0.0:8080->8080/tcp    yy

我有两个域名:www.xx.com和www.yy.com,分别通过nginx映射到xx容器和yy容器上,其中

xx容器映射的port是:8088

yy容器映射的port是:8080

我想,在同一台服务上处理两个域名的反向代理:

1.浏览器输入www.xx.com的时候,nginx转发到xx容器上处理web请求

2.浏览器输入www.yy.com的时候,nginx转发到yy容器上处理web请求

二、Nginx安装

1.docker搜索

# docker search nginx
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                             Official build of Nginx.                        12394               [OK]
jwilder/nginx-proxy               Automated Nginx reverse proxy for docker con…   1705                                    [OK]
richarvey/nginx-php-fpm           Container running Nginx + PHP-FPM capable of…   749                                     [OK]
linuxserver/nginx                 An Nginx container, brought to you by LinuxS…   84
bitnami/nginx                     Bitnami nginx Docker Image                      73                                      [OK]
tiangolo/nginx-rtmp               Docker image with Nginx using the nginx-rtmp…   60                                      [OK]
nginxdemos/hello                  NGINX webserver that serves a simple page co…   35                                      [OK]
jc21/nginx-proxy-manager          Docker container for managing Nginx proxy ho…   34
nginx/unit                        NGINX Unit is a dynamic web and application …   33
jlesage/nginx-proxy-manager       Docker container for Nginx Proxy Manager        31                                      [OK]
nginx/nginx-ingress               NGINX Ingress Controller for Kubernetes         22
privatebin/nginx-fpm-alpine       PrivateBin running on an Nginx, php-fpm & Al…   19                                      [OK]
schmunk42/nginx-redirect          A very simple container to redirect HTTP tra…   17                                      [OK]
nginxinc/nginx-unprivileged       Unprivileged NGINX Dockerfiles                  12
centos/nginx-18-centos7           Platform for running nginx 1.8 or building n…   12
blacklabelops/nginx               Dockerized Nginx Reverse Proxy Server.          12                                      [OK]
centos/nginx-112-centos7          Platform for running nginx 1.12 or building …   11
nginx/nginx-prometheus-exporter   NGINX Prometheus Exporter                       9
sophos/nginx-vts-exporter         Simple server that scrapes Nginx vts stats a…   6                                       [OK]
1science/nginx                    Nginx Docker images that include Consul Temp…   5                                       [OK]
mailu/nginx                       Mailu nginx frontend                            5                                       [OK]
pebbletech/nginx-proxy            nginx-proxy sets up a container running ngin…   2                                       [OK]
ansibleplaybookbundle/nginx-apb   An APB to deploy NGINX                          1                                       [OK]
centos/nginx-110-centos7          Platform for running nginx 1.10 or building …   0
wodby/nginx                       Generic nginx                                   0                                       [OK]

选择第一个即可

2.nginx配置

# mkdir -p /opt/docker/nginx/{conf.d,logs}
# touch /opt/docker/nginx/nginx.conf

nginx.conf: nginx的默认配置
logs: nginx日志目录
conf: 自定义的配置文件目录

配置nginx.conf(重点)

events {
    worker_connections 1024;
}

http {
    server {
        listen       80;
        server_name  xx.com;
        include /etc/nginx/conf.d/*.conf;
        location / {
            proxy_pass http://docker-ip:8088;
        }
    }

    server {
        listen 80;
        server_name yy.com;
        include /etc/nginx/conf.d/*.conf;
        location / {
            proxy_pass http://docker-ip:8080;
        }
    }
}

3.执行安装

# docker run --name nginx -p 80:80 
  -v /opt/docker/nginx/nginx.conf:/etc/nginx/nginx.conf 
  -v /opt/docker/nginx/conf.d:/etc/nginx/conf.d 
  -v /opt/docker/nginx/logs:/var/log/nginx 
  -d nginx
76510fc18b4316fcf40f0d4810e30e26548455f1779c28fc616894525eabad35

 三、说明

1.验证效果是,浏览器中输入www.xx.com时跳转到xx的docker容器处理web请求,当浏览器输入www.yy.com的时候,跳转到yy的docker容器处理web请求

2.我在一个坑里爬了很长时间,原因是很多资料不是docker的,所以配置的都是localhost,当nginx为docker容器的时候,localhost域名跳转不到xx和yy容器,必须通过docker的ip进行跳转到xx和yy容器

原文地址:https://www.cnblogs.com/Netsharp/p/12112579.html