必威-必威-欢迎您

必威,必威官网企业自成立以来,以策略先行,经营致胜,管理为本的商,业推广理念,一步一个脚印发展成为同类企业中经营范围最广,在行业内颇具影响力的企业。

我这里使用了daocloud加速器,这里我们拉取官方的

2019-09-21 22:29 来源:未知

1.7、nginx域名深入分析

命令表明:

default.conf

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #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;
    #}
}

重新开动nginx

[root@docker /]# docker stop my_nginx
my_nginx
[root@docker /]# docker rm my_nginx
my_nginx
[root@docker nginx]# docker run --name=my_nginx -v /u01/nginx/nginx.conf:/etc/nginx/nginx.conf -v /u01/nginx/conf.d:/etc/nginx/conf.d -p 8000:80 -d nginx
6efe91858f071a50197da104cdccf8500234f1bf6d0f4f56d3dc5de02261272c
[root@docker /]# curl http://192.168.43.32:8000
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
....

注意到推行了二个 docker rm my_nginx 命令,那是因为容器每便运营供给内定差异的名字,二个名字不能够利用频仍,能够由此docker ps -a查看全部容器,包涵未运转的器皿。rm命令能够去除钦定容器
下边正式布署方面四个情景

重启nginx镜像

docker restart mynginx

docker常用命令

1、甘休全数正在运营的器皿

docker kill $(docker ps -a -q)

2、删除全体已告一段落运作的容器

docker rm $(docker ps -a -q)

3、查看容器运营情况

docker stats

4、步向容器内进行命令行操作

docker exec -it content-name-or-id /bin/bash

大规模难题

CentOS7 情状下因为宿主的SELINUX,导致在nginx容器内不可能访谈配置文件(default.conf),进而容器一点都不大概提供web服务

消除方法:

#############方法一#############

笔记列表

   Brian

  153***2799@qq.com

流行官方MySQL(5.7.19)的docker镜像在开创时映射的安插文件目录有所不一样,在此记录并享受给我们:

官方原版的书文:

The MySQL startup configuration is specified in the file /etc/mysql/my.cnf, and that file in turn includes any files found in the /etc/mysql/conf.d directory that end with .cnf. Settings in files in this directory will augment and/or override settings in /etc/mysql/my.cnf. If you want to use a customized MySQL configuration, you can create your alternative configuration file in a directory on the host machine and then mount that directory location as /etc/mysql/conf.d inside the mysql container.

轮廓意思是说:

MySQL(5.7.19)的默许配置文件是 /etc/mysql/my.cnf 文件。假如想要自定义配置,提议向 /etc/mysql/conf.d 目录中开创 .cnf 文件。新建的文书能够轻便起名,只要保险后缀名是 cnf 就可以。新建的文本中的配置项能够覆盖 /etc/mysql/my.cnf 中的配置项。

具体操作:

首先须求创建将要映射到容器中的目录以及.cnf文件,然后再次创下建容器

# pwd
/opt
# mkdir -p docker_v/mysql/conf
# cd docker_v/mysql/conf
# touch my.cnf
# docker run -p 3306:3306 --name mysql -v /opt/docker_v/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 -d imageID
4ec4f56455ea2d6d7251a05b7f308e314051fdad2c26bf3d0f27a9b0c0a71414

命令表明:

  • -p 3306:3306:将容器的3306端口映射到主机的3306端口
  • -v /opt/docker_v/mysql/conf:/etc/mysql/conf.d:将主机/opt/docker_v/mysql/conf目录挂载到容器的/etc/mysql/conf.d
  • -e MYSQL_ROOT_PASSWORD=123456:开首化root顾客的密码
  • -d: 后台运营容器,并重返容器ID
  • imageID: mysql镜像ID

翻开容器运转状态

# docker ps
CONTAINER ID IMAGE          COMMAND          ... PORTS                    NAMES
4ec4f56455ea c73c7527c03a  "docker-entrypoint.sh" ... 0.0.0.0:3306->3306/tcp   mysql
server {      listen       80;      server_name  localhost;        #charset koi8-r;      #access_log  /var/log/nginx/log/host.access.log  main;        location / {          root   /data/nginx/html;         # root   /usr/nginx/html;          index  index.html index.htm;          autoindex  on;      try_files $uri /index/index/page.html;          #try_files $uri /index/map/page.html;      }        #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;      #}  }

3、查看容器运营情况

default.conf

server {
    listen       80;
    server_name localhost;
    location / {
        proxy_pass   http://tomcat_server;
    }
}

修改/u01/nginx/nginx.conf在http配置节点中扩充以下配置

upstream tomcat_server {
      server t1:8080;
    }

upstream 能够定义一组服务器
proxy_pass 设置代理的服务器,格式为http://upstream_name

重启nginx容器,这里我们需求采纳二个新的docker参数--link

[root@docker u01]# docker run --name=my_nginx1 --link=my_tomcat1:t1 -v /u01/nginx/nginx.conf:/etc/nginx/nginx.conf -v /u01/nginx/conf.d:/etc/nginx/conf.d -p 8000:80 -d nginx
[root@docker ~]# curl http://192.168.43.32:8000/WebTestApp
hello docker tomcat

ps:这里换了多个名字my_nginx1,用原本的名字my_nginx不或者运行容器,会报以下错误

2018/01/27 08:40:44 [emerg] 1#1: host not found in upstream "t1:8080" in /etc/nginx/nginx.conf:30
nginx: [emerg] host not found in upstream "t1:8080" in /etc/nginx/nginx.conf:30

t1不能够找到,但要是不应用--name钦命名称可平常运营,原因不详。
link参数可以在七个容器之间确立网络连接,格式为--link=my_tomcat1:t1 my_tomcat1为容器名称,t1为取的小名
可登陆nginx容器查看/etc/hosts文件,docker会将t1加入到hosts文件中

[root@docker ~]# docker exec -it my_nginx1 bash
root@fa5f782b9448:/# more /etc/hosts
127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.5  t1 1f9b1d432ab0 my_tomcat1
172.17.0.6  fa5f782b9448

1、使用docker加快器,小编这里运用了daocloud加快器。

运营容器

runoob@runoob:~/mysql$ docker run -p 3306:3306 --name mymysql -v $PWD/conf/my.cnf:/etc/mysql/my.cnf -v $PWD/logs:/logs -v $PWD/data:/mysql_data -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6
21cb89213c93d805c5bacf1028a0da7b5c5852761ba81327e6b99bb3ea89930e
runoob@runoob:~/mysql$ 

命令表达:

  • -p 3306:3306:将容器的3306端口映射到主机的3306端口

  • -v $PWD/conf/my.cnf:/etc/mysql/my.cnf:将主机当前目录下的conf/my.cnf挂载到容器的/etc/mysql/my.cnf

  • -v $PWD/logs:/logs:将主机当前目录下的logs目录挂载到容器的/logs

  • -v $PWD/data:/mysql_data:将主机当前目录下的data目录挂载到容器的/mysql_data

  • -e MYSQL_ROOT_PASSWORD=123456:伊始化root客户的密码

docker run --name mynginx -d -p 80:80 -v /data/nginx/html:/usr/share/nginx/html -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf  -v /data/nginx/logs:/var/log/nginx -v /data/nginx/conf.d:/etc/nginx/conf.d nginx

4、生成php测验文件info.php

nginx配置文件

走入nginx容器后台

[root@docker /]# docker exec -it my_nginx bash
root@1833fcff605b:/# cd /etc/nginx/
root@1833fcff605b:/etc/nginx# ls -l
total 36
drwxr-xr-x. 2 root root   26 Dec 26 18:16 conf.d
-rw-r--r--. 1 root root 1007 Dec 26 11:11 fastcgi_params
-rw-r--r--. 1 root root 2837 Dec 26 11:11 koi-utf
-rw-r--r--. 1 root root 2223 Dec 26 11:11 koi-win
-rw-r--r--. 1 root root 5170 Dec 26 11:11 mime.types
lrwxrwxrwx. 1 root root   22 Dec 26 11:11 modules -> /usr/lib/nginx/modules
-rw-r--r--. 1 root root  643 Dec 26 11:11 nginx.conf
-rw-r--r--. 1 root root  636 Dec 26 11:11 scgi_params
-rw-r--r--. 1 root root  664 Dec 26 11:11 uwsgi_params
-rw-r--r--. 1 root root 3610 Dec 26 11:11 win-utf

nginx.conf是nginx首要布署文件,可以由此more命令查看nginx.conf(容器默许不安装vi工具)

root@1833fcff605b:/etc/nginx# more nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
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;
}

静心到最后一行配置
include /etc/nginx/conf.d/*.conf;
include能够将其余安插文件导入,步向/etc/nginx/conf.d/目录下查看

root@1833fcff605b:/etc/nginx# cd /etc/nginx/conf.d/
root@1833fcff605b:/etc/nginx/conf.d# ls
default.conf

default.conf

root@1833fcff605b:/etc/nginx/conf.d# more default.conf
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #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;
    #}
}

#代表注释可以忽略
server:代表虚构服务器,上边会解释什么是编造服务器
server_name:服务器名称
location:访谈路径相称法规,这也是nginx最灵敏的地点,能够动用正则表明式
解释下怎么是编造服务器
举个例子给nginx服务器在dns上安排八个域名
domain1.nginx.com
domain2.nginx.com
客户访谈这两个域名,你大概希望她们寻访同三个后台服务,也说不定希望访问区别的后台服务
那便是说在nginx里就能够配备五个虚构服务器也等于多少个server节点

server {
    listen       80;
    server_name  domain1.nginx.com;
    ...
}
server {
    listen       80;
    server_name  domain2.nginx.com;
    ...
}

多少个虚构服务器监听同一个端口80.nginx能够依照顾客访问的host(http底部host)字段代理到不一样服务器上

location->root:静态财富目录,web静态财富如图片,js,html可透过该措施存放
location->index:如客户未指定央求能源名称,暗中同意访谈index内定的文本,如访谈http://host:port/html/则私下认可访谈http://host:port/html/index.html

查看php镜像的ip地址

docker inspect --format='{{.NetworkSettings.IPAddress}}' myphp

172.17.0.2

方法二、docker pull mysql

查找Docker Hub上的mysql镜像

runoob@runoob:/mysql$ docker search mysql
NAME                     DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                    MySQL is a widely used, open-source relati...   2529      [OK]       
mysql/mysql-server       Optimized MySQL Server Docker images. Crea...   161                  [OK]
centurylink/mysql        Image containing mysql. Optimized to be li...   45                   [OK]
sameersbn/mysql                                                          36                   [OK]
google/mysql             MySQL server for Google Compute Engine          16                   [OK]
appcontainers/mysql      Centos/Debian Based Customizable MySQL Con...   8                    [OK]
marvambass/mysql         MySQL Server based on Ubuntu 14.04              6                    [OK]
drupaldocker/mysql       MySQL for Drupal                                2                    [OK]
azukiapp/mysql           Docker image to run MySQL by Azuki - http:...   2                    [OK]
...

那边我们拉取官方的镜像,标签为5.6

runoob@runoob:~/mysql$ docker pull mysql:5.6

等候下载达成后,大家就能够在本地镜像列表里查到REPOSITOCRUISERY为mysql,标签为5.6的镜像。


user  nginx;worker_processes  1;error_log  /var/log/nginx/error.log warn;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;}

条件介绍

6. 场景2-tomcat代理

早为之所一个servet作为测验

package com.df.demo;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.*;
import javax.servlet.http.*;

public class WebTestService extends HttpServlet {
    private static final String CONTENT_TYPE = "text/html; charset=UTF-8";

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response) throws ServletException,
                                                           IOException {
        response.setContentType(CONTENT_TYPE);
        PrintWriter out = response.getWriter();
        out.print("hello docker tomcat");
        out.close();
    }
}

打成war包,包名称叫WebTestApp.war,在u01上成立webapps目录并上传war包

[root@docker webapps]# pwd
/u01/webapps
[root@docker webapps]# ls
WebTestApp.war

开发银行tomcat容器,并将/u01/webapps目录挂载到tomcat的/usr/local/tomcat/webapps目录下

[root@docker ~]# docker run --name=my_tomcat1 -v /u01/webapps:/usr/local/tomcat/webapps -p 8001:8080 -d tomcat
[root@docker ~]# curl http://localhost:8001/WebTestApp
hello docker tomcat

前后相继已经打响布置到tomcat上
修改/u01/nginx/conf.d/default.conf文件

预备干活

翻看容器运营状态

runoob@runoob:~/mysql$ docker ps 
CONTAINER ID    IMAGE         COMMAND                  ...  PORTS                    NAMES
21cb89213c93    mysql:5.6    "docker-entrypoint.sh"    ...  0.0.0.0:3306->3306/tcp   mymysql

Docker 安装 PHP

Docker 安装 Tomcat

1.3.2 vim /data/nginx/conf.d/default.conf

curl -sSL | sh -s

nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
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;
}

在宿主主机关闭SELINUX

使用mysql镜像

1.4 运营容器
#将容器中nginx的80端口映射到地面包车型客车81端口

2、删除全部已停止运行的器皿

1. 镜像安装

能够通过以下命令拉取nginx和tomcat镜像作为测量检验

[root@docker /]# docker pull nginx
[root@docker /]# docker pull tomcat

以特权格局运营容器

1.1 从hup上pull镜像
# docker pull nginx


5. 场景1-静态能源代理

在u01目录下创建目录resource,并上传静态财富

[root@docker resource]# pwd
/u01/resource
[root@docker resource]# ll
total 164
-rw-r--r--. 1 root root 147291 Oct  8  2015 angular.min.js
-rw-r--r--. 1 root root  17189 Nov  3 10:32 docker.jpg
[root@docker resource]#

修改/u01/nginx/conf.d/default.conf文件

curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f7cb09b5.m.daocloud.io

1.2 创制将在挂载的目录
# mkdir -p /data/nginx/{conf,conf.d,html,logs}

--name myphp:将容器命名叫myphp 

7. 场景3-tomcat负载均衡

  1. 修改/u01/nginx/nginx.conf在场景2的功底上加码新的服务器
upstream tomcat_server {
      server t1:8080;
      server t2:8080;
    }
  1. 在开发银行多个tomcat容器my_tomcat2
[root@docker ~]# docker run --name=my_tomcat2 -v /u01/webapps:/usr/local/tomcat/webapps -d tomcat
  1. 重启nginx
[root@docker /]# docker stop my_nginx1
[root@docker /]# dcoker rm my_nginx1
[root@docker /]# docker run --name=my_nginx1 --link=my_tomcat1:t1 --link=my_tomcat2:t2 -v /u01/nginx/nginx.conf:/etc/nginx/nginx.conf -v /u01/nginx/conf.d:/etc/nginx/conf.d -p 8000:80 -d nginx
[root@docker ~]# curl http://192.168.43.32:8000/WebTestApp
hello docker tomcat
  1. 能够分别登入七个tomcat容器查看access.log能够查看结果,nginx在两台服务器之间做轮询.轮询是nginx私下认可的载荷均衡格局。

nginx相关目录:var/mydocker/nginx/conf.d

1.3 先要有布署文件能力开发银行容器
1.3.1 vim /data/nginx/conf/nginx.conf

遍布难点

2. nginx启动

  1. 镜像下载完后,实行以下命令运转nginx
[root@docker /]# docker run --name=my_nginx -p 8000:80 -d nginx

--name: 为nginx容器内定叁个称谓方便管理
-p: 将nginx内部80端口代理到宿主机七千端口,能够透过宿主机:8000访问nginx 80端口
-d: 后台运行

  1. 能够经过docker ps 命令查看容器运维情状
[root@docker /]# docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                            NAMES
1833fcff605b        nginx                 "nginx -g 'daemon off"   2 minutes ago       Up 2 minutes        0.0.0.0:8000->80/tcp             my_nginx
  1. 浏览器访问http://192.168.43.32:8000/翻开是不是能够访问,也许应用curl命令(推荐)
[root@docker /]# curl http://192.168.43.32:8000
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
....

以下为示范内容

 server {

   listen  80 default_server;

   server_name _;

   root  /usr/share/nginx/html;

   location / {

   index index.html index.htm index.php;

   autoindex off;

   }

   location ~ .php(.*)$ {

   root  /var/www/html/;

   fastcgi_pass 172.17.0.2:9000;

   fastcgi_index index.php;

   fastcgi_split_path_info ^((?U).+.php)(/?.+)$;

   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

   fastcgi_param PATH_INFO $fastcgi_path_info;

   fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;

   include  fastcgi_params;

   }

 }

搭建境遇

1、启动php镜像

docker run -p 9000:9000 --name myphp 

-v /var/mydocker/nginx/www/:/var/www/html/ 

--privileged=true 

-d php:5.6-fpm

一声令下表达:

-p 9000:9000:将容器的玖仟端口映射到主机的8000端口

--name myphp:将容器命名字为myphp

-v -v /var/mydocker/nginx/www/:/var/www/html/ :将主机中/var/mydocker/nginx/www挂载到容器的/var/www/html

--privileged=true:container内的root具备真正的root权限


server {        listen       80;        server_name  erpshopwebservertest.xinyartech.com;        root   /usr/share/nginx/html/test/;        access_log  /usr/share/nginx/html/test/.log  main;        #log_format access '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent $request_body "$http_referer" "$http_user_agent" $http_x_forwarded_for';        error_log   /usr/share/nginx/html/test/pay_local.error;    client_max_body_size 60M;    client_body_buffer_size 512k;    location / {        proxy_pass      http://172.1.22.25:8082;                proxy_redirect  off;                proxy_set_header Host $host;                proxy_set_header X-Real-IP $remote_addr;                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;        }       # rewrite ^ https://$server_name$1 permanent;}

SELINUX=disabled

default.conf

server {
    listen       80;
    server_name localhost;
    location /resource {
        root   /u01;
        index  index.html index.htm;
    }
}

ps:root是/u01不是/u01/resource。假设root配置成/u01/resource/则nginx会去/u01/resource/resource目录下搜寻文件
再也启航nginx容器,将resource目录挂载到容器内部

[root@docker u01]# docker stop my_nginx
my_nginx
[root@docker u01]# docker rm my_nginx
my_nginx
[root@docker u01]# docker run --name=my_nginx -v /u01/nginx/nginx.conf:/etc/nginx/nginx.conf -v /u01/nginx/conf.d:/etc/nginx/conf.d -v /u01/resource:/u01/resource -p 8000:80 -d nginx

访问
http://192.168.43.32:8000/resource/docker.jpg
http://192.168.43.32:8000/resource/angular.min.js
测量试验配置是不是成功(将方面ip替换来宿主机ip)

根目录: var/mydocker/nginx

1.6 网页访谈nginx
# curl

#重启docker

3. 测量试验场景

大家只要有以下多个现象

  1. 场景1
    宿主机有个目录存储静态财富,供给通过nginx代理出去,顾客访谈http://host:port/resource/xxxx访问
  2. 场景2
    tomcat服务器(docker容器)上运行叁个web程序,须求经过nginx代理出去,web程序context root为WebTestApp
  3. 场景3
    该web程序运维在多个tomcat容器中,供给经过nginx做负载均衡

演示内容如下

server {

listen 80;

server_name www.test.com;

root /usr/share/nginx/html/test.com/;

location / {

index index.html index.htm index.php;

autoindex off;

}

location ~ .php(.*)$ {

root /var/www/html/test.com/;

fastcgi_pass 172.17.0.2:9000;

fastcgi_index index.php;

fastcgi_split_path_info ^((?U).+.php)(/?.+)$;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_param PATH_INFO $fastcgi_path_info;

fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;

include fastcgi_params;

}

}

1.1 从hup上pull镜像
1.2 创制就要挂载的目录
1.3 先要有安插文件技术开行容器
1.3.1 vim /data/nginx/conf/nginx.conf
1.3.2 vim /data/nginx/conf.d/default.conf
1.4 运维容器
1.5 查看运营的器皿
1.6 网页访谈nginx

网址根目录:var/mydocker/nginx/www

4. nginx配置文件挂载

docker容器每趟重启都是三个新的条件,也便是说在docker容器内做的别的修改都将被复苏,但nginx的配置文件是在docker容器内,我们只要一向进行修改每一次重启后都会被恢复生机,那并非大家所愿意的,所以首先须要将铺排文件从docker容器中"搬到"宿主机上,这里会经过docker的卷(volume)达成。
volume能够将地点文件挂载到docker容器内,那样容注重启后消息不会放弃
对于nginx,能够将nginx.conf文件和conf.d目录从容器内部"搬"出来
在地面创制nginx.conf文件和conf.d目录

[root@docker /]# mkdir -p /u01/nginx
[root@docker /]# mkdir -p /u01/nginx/conf.d
[root@docker /]# touch /u01/nginx/nginx.conf
[root@docker /]# touch /u01/nginx/conf.d/default.conf

nginx.conf文件和default.conf内容能够直接从容器内部复制

情况介绍

1.5 查看运维的器皿
# docker ps

  listen  80;

--privileged参数为true

docker run -it --privileged=true -d nginx

# 示例内容如下

永世关闭 修改/etc/sysconfig/selinux文件

SELINUX=disabled

#############方法二#############

  fastcgi_index index.php;

重启docker

service docker restart

2、下载官方相关镜像

docker pull nginx
docker pull php:5.6-fpm

3、建设构造有关目录

mkdir -p /var/mydocker/nginx/www
mkdir -p /var/mydocker/nginx/conf.d

4、编辑default.conf

vim /var/mydocker/nginx/conf.d/default.conf

4、编辑default.conf

网址根目录:var/mydocker/nginx/www

3、建构有关目录

临时关闭

setenforce 0

消除方法:

修改default.conf配置文件,使fastcgi_pass的值为 172.17.0.2:9000

vim /var/mydocker/nginx/conf.d/default.conf

fastcgi_pass 172.17.0.2:9000;

2、启动nginx镜像

docker run -p 80:80 --name mynginx

-v /var/mydocker/nginx/www:/usr/share/nginx/html

-v /var/mydocker/nginx/conf.d:/etc/nginx/conf.d

--privileged=true

-d nginx

3、查看镜像运维状态

docker ps

[图形上传失败...(image-ffc82f-1514356281458)]

4、生成php测验文件info.php

echo " /docker/www/info.php

浏览器访谈 http://localhost/info.php 验证

nginx设想机配置

以配置www.test.com设想机为例,项目目录地址为/docker/www/test.com/

vim /docker/nginx/conf.d/test.com.conf

-d nginx

#权且关门

TAG标签:
版权声明:本文由必威发布于必威-编程,转载请注明出处:我这里使用了daocloud加速器,这里我们拉取官方的