linux(centos) docker install nginx

​1. Pull the latest version of nginx image

docker pull nginx:latest

view mirror

docker images or docker images -a  

  

2. Start the nginx container


docker run -d -p 80:80 --name nginx nginx
 

Use the docker run command to start the nginx container.

  • --name, set the container name. For the convenience of memory, set the name to nginx
  • -d, run in the background.
  • -p, port mapping, maps the container port to the host port. Deployed on port 80 by default

 

 3. Modify the nginx listening port

If you don't want nginx to listen to the default port 80, you can use the -p option to modify the mapped port.

docker run -d -p 8080:80 --name nginx nginx

4. Modify configuration and content

If you want to modify the configuration or content of nginx, you can use the -v option for data volume mapping.
The configuration content of nginx mainly includes:

  • Configuration, default location: /etc/nginx/nginx.conf.
  • Website, default location: /usr/share/nginx.
  • Module, default location: /etc/nginx/modules.

We set up the website on the host machine and display it to nginx through data volume mapping. The command is as follows:

docker run -d -p 8080:80 -v 宿主机绝对路径:/usr/share/nginx/html  --name nginx nginx

Using data volume mapping, you can modify the default configuration of nginx and load websites. At the same time, once the container is created, it cannot be modified. Temporary data in the container disappears once the container is closed. The use of data volume mapping also solves the problem of data persistence. 

// 启动容器
docker run -d -p 80:80 nginx
// 创建容器挂载路径
mkdir -p /data/nginx/{conf,conf.d,html,logs}
// 查看容器ID
docker ps -a
// 复制配置文件到挂载路径


docker cp nginx:/etc/nginx/nginx.conf /data/nginx/conf/
docker cp nginx:/etc/nginx/conf.d/default.conf /data/nginx/conf.d/
docker cp nginx:/usr/share/nginx/html /data/nginx/html
// 关闭容器
docker stop nginx
// 删除容易
docker rm nginx
// 以挂载的方式启动容器
docker run -id --name=nginx \
-p 8080:80 \
-v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /data/nginx/conf.d:/etc/nginx/conf.d \
-v /data/nginx/logs:/var/log/nginx \
-v /data/nginx/html:/usr/share/nginx/html \
-d nginx


当docker跑nginx镜像时,设置端口映射,则只有该映射端口起作用,nginx配置的其他端口无效 所以只对80 生效
如果想使用多个端口需要
// 以挂载的方式启动容器
docker run -id --name=nginx \
-p 80:80 \
-p 8085:8085 \
-v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /data/nginx/conf.d:/etc/nginx/conf.d \
-v /data/nginx/logs:/var/log/nginx \
-v /data/nginx/html:/usr/share/nginx/html \
-d nginx


// 以挂载的方式启动容器   https  证书目录
docker run -id --name=nginx \
-p 80:80 \
-p 8085:8085 \
-v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /data/nginx/conf.d:/etc/nginx/conf.d \
-v /data/nginx/logs:/var/log/nginx \
-v /data/nginx/html:/usr/share/nginx/html \
-v /data/nginx/cert:/etc/nginx -d nginx  \
-d nginx
#配置 nginx.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;
    #gzip  on;
    include /etc/nginx/conf.d/*.conf;
}
# 配置default.conf
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;
   #解决Router(mode: 'history')模式下,刷新路由地址不能找到页面的问题
  location / {
         # docker 使用容器地址才能找到文件 自动映射到挂载目录
         root   /usr/share/nginx/html;
        index  index.html index.htm;
        if (!-e $request_filename) {
            rewrite ^(.*)$ /index.html?s=$1 last;
            break;
        }
    }
#后台服务配置,配置了这个location便可以通过http://域名/jeecg-boot/xxxx 访问      
 location ^~ /shimmer{
        proxy_pass              http://127.0.0.1:7001/shimmer/;# 如果是用docker请填写真实IP 因为容器内部访问不到本127.0.0.1或locahost
        proxy_set_header        Host 127.0.0.1;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    #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;
    }
}

5. Static resource deployment:

Place static resources in the ~/nginx/html directory to access them.

6. Port binding:

vim ~/nginx/conf.d/static.conf

One port configures one project

server {
    listen 81; # 监听的端⼝
    server_name localhost; # 域名或ip
    location / { # 访问路径配置
        root /usr/share/nginx/index;# 根⽬录
        index index.html index.htm; # 默认⾸⻚
    }
     error_page 500 502 503 504 /50x.html; # 错误⻚⾯
         location = /50x.html {
         root html;
     }
 }

7. Domain name binding:

server {
    listen 81; # 监听的端⼝
    server_name www.ahunag.com; # 域名或ip
    location / { # 访问路径配置
        root /usr/share/nginx/index;# 根⽬录
        index index.html index.htm; # 默认⾸⻚
    }
     error_page 500 502 503 504 /50x.html; # 错误⻚⾯
         location = /50x.html {
         root html;
     }
 }

8. Reverse proxy

upstream tomcat-kkb{
     server 192.168.220.12:8080;
 }
 server {
    listen 80; # 监听的端⼝
    server_name www.kkb.com; # 域名或ip
    location / { # 访问路径配置
    root index;# 根⽬录
        proxy_pass http://tomcat-kkb;
        index index.html index.htm; # 默认⾸⻚
    }
 }

9. Load balancing: (default is rotation training) set weight

nginx upstream tomcat-kkb

{ server 192.168.220.12:8888 weight=2;

server 192.168.220.12:8080;

}

10. Solve front-end AJax cross-domain problems

ajax请求:url: “/test”

使用代理到->192.168.111.135:8888/test

server{
    listen 81;
    location / {
        root /usr/share/nginx;
        index  test.html;
     }
location /test {
        proxy_pass http://192.168.111.135:8888/test;
     }
}

11. https configuration

// 以挂载的方式启动容器   https  证书目录
docker run -id --name=nginx \
-p 80:80 \
-p 8085:8085 \
-p 443:443 \
-v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /data/nginx/conf.d:/etc/nginx/conf.d \
-v /data/nginx/logs:/var/log/nginx \
-v /data/nginx/html:/usr/share/nginx/html \
-v /data/nginx/cert:/etc/nginx/cert  \
-d nginx

这种配置需要重



济南服务

命令  描述
docker pull nginx   下载最新版Nginx镜像 (其实此命令就等同于 : docker pull nginx:latest )
docker pull nginx:xxx   下载指定版本的Nginx镜像 (xxx指具体版本号)

# 创建挂载目录
mkdir -p /docker/nginx/conf
mkdir -p /docker/nginx/log
mkdir -p /docker/nginx/html


# 生成容器
# 将容器nginx.conf文件复制到宿主机
# 将容器conf.d文件夹下内容复制到宿主机
# 将容器中的html文件夹复制到宿主机
docker run --name nginx -p 80:80 -d nginx

docker cp nginx:/etc/nginx/nginx.conf /data/docker/nginx/conf/nginx.conf

docker cp nginx:/etc/nginx/conf.d /data/docker/nginx/conf/conf.d

docker cp nginx:/usr/share/nginx/html /data/docker/nginx/


# 直接执行docker rm nginx或者以容器id方式关闭容器
# 找到nginx对应的容器id
docker ps -a
# 关闭该容器
docker stop nginx
# 删除该容器
docker rm nginx

# 删除正在运行的nginx容器
docker rm -f nginx

12. Automatic start


The --restart option of docker can realize automatic startup, and its optional parameters include:

  • no, do not automatically restart the container. default.
  • on-failure When the exit status of the container is not 0, restart the container. A maximum number of restarts can be specified.
  • unless-stopped Always restart containers when they exit, but disregard containers that were not started when the Docker daemon started.
  • always keeps the container started at all times. With the docker service, it can be automatically started at boot.
sudo systemctl enable docker.service
docker run -d  --restart=always  nginx

If the nginx container is already started, it can be updated with docker update.

docker update --restart=always nginx

Guess you like

Origin blog.csdn.net/swebin/article/details/132010098