1. 程式人生 > 其它 >dcoker 安裝配置nginx

dcoker 安裝配置nginx

技術標籤:dockernginx

這裡寫自定義目錄標題

安裝nginx

1.下載nginx映象

docker pull nginx:stable

2.啟動nginx容器


docker run -d -p 80:80 -p 443:443 --name nginx \
  -v /home/nginx/www:/usr/share/nginx/html \
  -v /home/nginx/conf:/etc/nginx \
  -v /home/nginx/logs:/var/log/nginx \
  nginx:stable

-p :將容器內部使用的網路埠隨機對映到我們使用的主機上。80是http,443是https
-v: 把重要的目錄掛載到主機上,
這裡在主機上新建了www,conf,logs三個目錄,分別掛載nginx的html ,配置檔案,和日誌

配置nginx

由於配置檔案都掛載到了home/nginx/conf目錄,這裡主要修改他

轉發請求

home/nginx/conf/nginx.conf預設的檔案如下

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
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;
}

include /etc/nginx/conf.d/*.conf;:conf.d目錄的所有配置檔案都被引用進來,因此我在conf.d 新建一個xxx.conf配置檔案來轉發xxx.com域名的請求

//xxx.conf
server {
	listen	80; //監聽nignx80埠
	server_name	xxx.xxx.com;//監聽請求的域名
	location / {
		proxy_pass http://49.234.xxx.xxx:8080/; //轉發目的
	}
}

儲存檔案之後,docker restart nginx 重啟容器

然後 在瀏覽器輸入xxx.xxx.com 就被會nginx捕捉到並且轉發到http://49.234.xxx.xxx:8080

新增https

server {
	listen	80;
	listen	443 ssl;
	server_name		xxx.xxx.com;//監聽請求的域名
	ssl_certificate xxx.xxx.com_bundle.crt;
	ssl_certificate_key xxx.xx.com.key;
	ssl_session_timeout 5m;
	ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
	ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; 
	ssl_prefer_server_ciphers on;
	location / {
		proxy_pass http://49.234.xxx.xxx:8080/; //轉發目的
	}

}

注意私鑰xxx.xx.com.key 和證書xxx.xxx.com_bundle.crt 放在/home/nginx/conf目錄下
重啟nginx即可。

參考連結: Nginx 伺服器證書安裝