> 文章列表 > Docker安装Nginx

Docker安装Nginx

Docker安装Nginx

Docker安装Nginx

  • 1、安装步骤
    • 1.1 服务器
    • 1.2 下载镜像
    • 1.3 创建挂载目录
    • 1.4 创建默认配置文件
    • 1.5 创建测试配置文件
    • 1.6 创建测试html页面
    • 1.7 启动容器
    • 1.8 访问测试
    • 1.9 Nginx配置文件参考示例

1、安装步骤

1.1 服务器

至少一台服务器,以下安装环境:

服务器:Linux
系统:CentOS Linux release 7.6.1810 (Core)

1.2 下载镜像

# 外网环境直接执行
docker pull nginx# 内网环境
docker save -o /data/nginx/nginx.tar nginx # 在外网环境下载镜像后保存
docker load -i /data/nginx/nginx.tar	   # 拷贝到内网机,加载镜像

1.3 创建挂载目录

mkdir -p /data/nginx/{conf,conf.d,html,logs}

1.4 创建默认配置文件

vi /data/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;
}

1.5 创建测试配置文件

vi /data/nginx/conf.d/default.conf

server {  listen       80;  server_name  localhost;  #charset koi8-r;  #access_log  /var/log/nginx/log/host.access.log  main;  location / {  #root   /data/nginx/html;  root   /usr/share/nginx/html;  index  index.html index.htm;  #autoindex  on;  #try_files $uri /index/index/page.html;  #try_files $uri /index/map/page.html;  }  #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;  #}  
}

1.6 创建测试html页面

vi /data/nginx/html/index.html

<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title>Nginc测试静态页面</title>
</head>
<body>
<div id="datetime"><script>setInterval("document.getElementById('datetime').innerHTML=new Date().toLocaleString();", 1000);</script>
</div>
</body>
</html>

1.7 启动容器

docker run --restart=always --name nginx-dev -d -p 80:80 -v /data/nginx/html:/usr/share/nginx/html -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf  -v /data/nginx/conf.d:/etc/nginx/conf.d  -v /data/nginx/logs:/var/log/nginx  -d nginx:latest

参数说明:

--name nginx-dev										# 容器名称
-p 80:80 			   									# 映射端口
-v /data/nginx/html:/usr/share/nginx/html 				# 挂载静态文件地址
-v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf 	# 挂载默认配置文件
-v /data/nginx/conf.d:/etc/nginx/conf.d  				# 挂载配置文件地址
-v /data/nginx/logs:/var/log/nginx  					# 挂载日志文件地址

1.8 访问测试

http://10.xx.xx.xx

1.9 Nginx配置文件参考示例

server {listen       80;server_name  localhost;gzip on;gzip_comp_level    8;gzip_min_length    1K;gzip_buffers 32 4k;gzip_http_version 1.1;gzip_static on;gzip_proxied       any;  gzip_vary          on;gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript 		   application/x-httpd-php application/x-font-ttf;gzip_disable "MSIE [1-6]\\.";#charset koi8-r;#access_log  logs/host.access.log  main;root  /usr/share/nginx/html/dist;client_max_body_size 200m;client_header_buffer_size 512k;large_client_header_buffers 4 512k;location / {try_files $uri $uri/ @router;index  index.html index.htm;}location @router {rewrite ^.*$ /index.html last;}# API接口location /api/ {rewrite  ^/api//(.*)$ /$1 break;proxy_pass   http://192.168.0.118:8080/;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}# 报表location /report/ {rewrite  ^/report//(.*)$ /$1 break;proxy_pass   http://192.168.0.119:8080/report/;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}#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   html;}
}