1 Installation in Docker environment

1.1 Downloading the Nginx Docker Image

docker pull nginx
Copy the code

1.2 Copying container Nginx Default Configuration

  • Run the container first (to copy configuration files) :
docker run -p 80:80 --name nginx -v /usr/local/docker/nginx/html:/usr/share/nginx/html -v /usr/local/docker/nginx/logs:/var/log/nginx -d nginx:latest
Copy the code
  • Copy the configuration file in the container to the specified directory
docker container cp nginx:/etc/nginx /usr/local/docker/nginx/
cd /usr/local/docker/nginx
mv nginx conf
Copy the code
  • Terminates and deletes the container
docker stop nginx
docker rm nginx
Copy the code
  • Go to the nginx configuration directory
cd /usr/local/docker/nginx/conf/conf.d/
ls
Copy the code

  • I use Minio object storage as an example, create minio.conf and configure it as follows
server { listen 443 ssl; server_name www.example.top; Client_max_body_size 64M; fastcgi_read_timeout 3600; error_page 500 502 503 504 /50x.html; root /usr/share/nginx/html; try_files $uri $uri/ @rewrite; # ssl on; ssl_certificate /etc/nginx/conf.d/certs/www.example.top/example.pem; ssl_certificate_key /etc/nginx/conf.d/certs/www.example.top/example.key; ssl_session_timeout 5m; Ssl_protocols TLSv1 TLSv1.1 TLSv1.2; Ssl_ciphers ecdhe-rSA-aes128-GMM-sha256 :HIGH:! Ssl_ciphers ecdhe-rSA-aes128-GMm-sha256 :HIGH:! aNULL:! MD5:! RC4:! DHE; Ssl_prefer_server_ciphers on; location / { add_header Content-Security-Policy upgrade-insecure-requests; proxy_set_header HOST $host; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; Proxy_pass http://172.17.0.5:9001/; }}Copy the code
  • Certificate need to generate its own or buy a third party, downloaded into/usr/local/docker/nginx/conf/conf. D/certs directory

  • At the same time, we changed the default configuration default.conf to use HTTP to automatically transfer to HTTPS

server { listen 80; server_name www.sparksys.top; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; rewrite ^(.*)$ https://$host$1 permanent; add_header Content-Security-Policy upgrade-insecure-requests; 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; #}}Copy the code

A focus onrewrite ^(.*)host$1 permanent;To achieve HTTPS automatic conversion

1.3 Start the docker-compose deployment

  • Create docker-comemess. yaml with the following configuration:
Version: '3.1' services: nginx: image: nginx:latest Container_name: nginx restart: always ports: - 80:80 - 443:443 volumes: - /usr/local/docker/nginx/conf:/etc/nginx - /usr/local/docker/nginx/logs:/var/log/nginx - /usr/local/docker/nginx/html:/usr/share/nginx/htmlCopy the code

We need to expose ports 80 and 443 before the firewall closes or opens ports 80 and 443

  • Deployed nginx
docker-compose up -d
Copy the code
  • Look at the container in which the Docker is running
docker ps -a
Copy the code

  • Enter the domain address in your browser

Docker and docker-compose successfully deploy nginx