Pull the Nginx image

docker pull nginx
Copy the code

Create an nginx container

docker run --name nginx -d \
-p 80:80 -p 443:443 \ 
-v /usr/local/docker/nginx/html:/usr/share/nginx/html:ro \
-v /usr/local/docker/nginx/logs:/var/log/nginx/:rw \
-v /usr/local/docker/nginx/config/conf.d:/etc/nginx/conf.d:rw \
-d nginx
Copy the code

Note:

  • /usr/local/ docker-nginx /conf /conf /conf /conf /conf /conf /conf /conf /conf /conf /conf /conf /conf /conf /conf

Enter the nginx container

docker exec -it nginx bash
Copy the code

Go to /etc/nginx and look at the nginx.conf configuration file, which is the root configuration file for nignx

cd /etc/nginx

cat nginx.conf
Copy the code

Nginx. conf contains the following contents:

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; /etc/nginx/conf. D /nginx/confCopy the code

Exit the nginx container, create default.conf in the host mapping configuration file directory, and edit it

cd /usr/local/docker/nginx/config/conf.d

vi default.conf
Copy the code

The contents of default.conf are as follows:

server { listen 80; server_name _; # _ represents the current host address #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; # nginx proxy of the root directory, I was mapped to/usr/local/docker/nginx/HTML, so I can create index under the directory. The HTML as the default web page index index. The HTML index. HTM; } # proxy configuration location /business {proxy_pass http://business.app.com; } # proxy configuration location /user {proxy_pass http://user.app.com; }}Copy the code

Enter/usr/local/docker/nginx/HTML, a new index. HTML

cd /usr/local/docker/nginx/html

vi index.html
Copy the code

The contents of index.html are as follows:

welcome to nginx
Copy the code

Check that the nginx configuration file is correct

// Check docker exec nginx nginx -tCopy the code

There is no problem with the file content.

Reloading the nginx configuration file is equivalent to restarting the Nginx container

 docker exec nginx nginx -s reload
Copy the code

Access nginx on port 80

192.168.57.128
Copy the code