Install Nginx

  1. Pull the Nginx image
docker pull nginx:latest
Copy the code
  1. Run a temporary container for copying internal config files
docker run --rm -d --name nginx:latest nginx-temp
Copy the code
  1. Generate nginx configurations, static resource directories, and log-related directory mappings on the host
mkdir -p ./nginx/{conf,html,logs}
Copy the code
  1. Put the container insidenginx.confdefault.confTo the host machine/confIn the directory
docker cp nginx-temp:/etc/nginx/nginx.conf ./
docker cp nginx-temp:/etc/nginx/conf.d/default.conf ./conf/
Copy the code
  1. Deleting temporary containers
Docker stop nginx-temp # Docker stop nginx-temp # docker stop nginx-temp #Copy the code
  1. Run the new nginx container
docker run -d --name nginx -p 80:80 -v /root/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /root/nginx/conf:/etc/nginx/conf.d -v /root/nginx/html:/usr/share/nginx/html -v /root/nginx/logs:/var/log/nginx - ring = true nginx: latest # - v: according to the Shared between host and container directory # - ring = true inside the container to mount the directory has read and write privileges, etcCopy the code
  1. in/root/nginx/htmlCreate a directoryindex.htmlFile and then access the host IP to access the page.

Basic configuration of Nginx

  1. Check the nginx. Conf
cat nginx/nginx.conf
Copy the code
# Initial configuration
main                                # global configuration
events {                            # Nginx working mode configuration
}
http {                                # set the HTTP
    .

    server {                        # server host configuration
        .
        location {                    # Route configuration
            .
        }

        location path {
            .
        }

        location otherpath {
            .}}server {
        .

        location {
            .}}upstream name {                    Load balancing configuration
        .}}Copy the code
  1. Configure access to static resources
server{ listen 80; server_name localhost; # public IP address or domain name index index.html index.htm; location / { root /usr/share/nginx/html index index.html index.htm } }Copy the code
  1. Domain name forward
server{ listen 80; server_name test.site.com; index index.html index.htm; location / { proxy_pass http://www.baidu.com; proxy_set_header Host $proxy_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }}Copy the code
  1. Port forwarding
server{ listen 80; server_name test.site.com; index index.html index.htm; Location /express/ {proxy_pass http://127.0.0.1:8080; proxy_set_header Host $proxy_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }}Copy the code

Proxy_pass specifies whether to add/to the address after proxy_pass

  • add/Represents the root absolute path:Test.site.com/express/ind…Forwarded to thehttp://127.0.0.1:8080/index.html
  • Do not add/: represents the relative path:Test.site.com/express/ind…Forwarded to thehttp://127.0.0.1:8080/express/index.html
  1. Load Balancing Configuration
Upstream backend {server 127.0.0.1:81; Server 127.0.0.1:82; } # in server: location / {proxy_pass http://backend; }Copy the code