Docker學習 - nginx配置

2021/10/25 7:09:29

本文主要是介绍Docker學習 - nginx配置,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.不挂載volumn的方式啓動image docker run -d -p 80:80 --name nginx-test nginx 2.拷貝文件完後停止container docker cp nginx-test:/etc/nginx /usr/local/docker/nginx/conf docker stop 818310423db6 docker rm 818310423db6 目錄結構如下

[centos@centos7 ~]$ tree /usr/local/docker
/usr/local/docker
└── nginx
├── conf
│   ├── conf.d
│   │   └── default.conf
│   ├── fastcgi_params
│   ├── mime.types
│   ├── nginx.conf
│   ├── scgi_params
│   └── uwsgi_params
├── html
│   └── index.html
└── logs
├── access.log
└── error.log

3.挂載本地目錄啓動container docker run -it -p 80:80 -v /usr/local/docker/nginx/conf:/etc/nginx \ -v /usr/local/docker/nginx/html:/usr/share/nginx/html \ -v /usr/local/docker/nginx/logs:/var/log/nginx \ --privileged=true --name mynginx 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" $upstream_addr $upstream_response_time $request_time ';

    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;

    #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;
    }
    
    location /apptst/ {
        proxy_pass  http://192.168.0.109:8080/;
        proxy_set_header  Host              $http_host;   # required for docker client's sake
        proxy_set_header  X-Real-IP         $remote_addr; # pass on real client's IP
        proxy_set_header  X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header  X-Forwarded-Proto $scheme;
    }
}

  

  

  4.改動了/usr/local/docker/nginx/conf/conf.d/default.conf后,要重啓container才生效 docker stop a1b912fc24a4 docker start a1b912fc24a4   5.將app啓動在兩個端口上做負載均衡 default.conf  
upstream apptst_server{
  server 192.168.0.109:8080 weight=1;
  server 192.168.0.109:8081 weight=1;
}


server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #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;
    }
    
    location /apptst/ {
        proxy_set_header  Host              $http_host;   # required for docker client's sake
        proxy_set_header  X-Real-IP         $remote_addr; # pass on real client's IP
        proxy_set_header  X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header  X-Forwarded-Proto $scheme;
       # proxy_pass http://192.168.0.109:8080/;
        proxy_pass http://apptst_server/;
    }
    

}

  



这篇关于Docker學習 - nginx配置的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程