1、获取Nginx镜像
查找Nginx镜像
docker search nginx
以下是返回的镜像信息:
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 11575 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1617 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 719 [OK]
bitnami/nginx Bitnami nginx Docker Image 67 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 63
tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 47 [OK]
nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 20
schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 17 [OK]
arm64v8/nginx Official build of Nginx. 16
nginxdemos/hello NGINX webserver that serves a simple page co… 15 [OK]
crunchgeek/nginx-pagespeed Nginx with PageSpeed + GEO IP + VTS + more_s… 12
blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. 12 [OK]
centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 10
centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 9
webdevops/nginx Nginx container 8 [OK]
nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 6
sophos/nginx-vts-exporter Simple server that scrapes Nginx vts stats a… 5 [OK]
nginx/nginx-prometheus-exporter NGINX Prometheus Exporter 4
1science/nginx Nginx Docker images that include Consul Temp… 4 [OK]
mailu/nginx Mailu nginx frontend 3 [OK]
pebbletech/nginx-proxy nginx-proxy sets up a container running ngin… 2 [OK]
travix/nginx NGinx reverse proxy 2 [OK]
wodby/nginx Generic nginx 0 [OK]
ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 0 [OK]
centos/nginx-110-centos7 Platform for running nginx 1.10 or building … 0
下载Nginx镜像
#获取最新的镜像
docker pull nginx
2、查看下载的Nginx镜像
docker images nginx
3、宿主机创建数据目录
在/data中建立Nginx的数据目录data、配置目录config、日志目录log,此处实例名称为scloud_nginx
mkdir -p /data/nginx /data/nginx/data /data/nginx/data/scloud_nginx /data/nginx/config /data/nginx/config/scloud_nginx /data/nginx/log /data/nginx/log/scloud_nginx
4、nginx配置文件及网站文件
创建nginx配置文件,上传网站文件到宿主机nginx实例scloud_nginx数据目录/data/nginx/data/scloud_nginx
5、启动Nginx实例
启动一个实例名为scloud_nginx的Nginx容器,端口号为8080
docker run \
-p 8080:8080 \
-v /data/nginx/data/scloud_nginx:/usr/share/nginx/html:rw \
-v /data/nginx/config/scloud_nginx/nginx.conf:/etc/nginx/nginx.conf:rw \
-v /data/nginx/log/scloud_nginx:/var/log/nginx:rw \
--name scloud_nginx \
-d nginx
命令说明:
-p 8080:8080 :将容器的8080端口映射到主机的8080端口
-v /data/nginx/data/scloud_nginx:/usr/share/nginx/html:rw \ : 使用宿主机中的网站目录
-v /data/nginx/config/scloud_nginx/nginx.conf:/etc/nginx/nginx.conf:rw \ : 使用宿主机中的nginx.conf配置文件
-v /data/nginx/log/scloud_nginx:/var/log/nginx:rw \ : 使用宿主机中的日志目录
5、Nginx增加端口8080端口放行
iptables -I INPUT 1 -p tcp --dport 8080 -j ACCEPT -m comment --comment "scloud nginx port"
service iptables save
systemctl restart iptables
iptables -L -n -v
6、Nginx参考配置文件nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#error_log "pipe:rollback logs/error_log interval=1d baknum=7 maxsize=2G";
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
# load modules compiled as Dynamic Shared Object (DSO)
#
#dso {
# load ngx_http_fastcgi_module.so;
# load ngx_http_rewrite_module.so;
#}
http {
include 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 logs/access.log main;
#access_log "pipe:rollback logs/access_log interval=1d baknum=7 maxsize=2G" main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#开启Gzip
gzip on;
#不压缩临界值,大于1K的才压缩,一般不用改
gzip_min_length 1k;
#buffer,不知道,反正不用修改
gzip_buffers 4 16k;
#压缩级别,1-10,数字越大压缩的越好,时间也越长,看心情随便改吧
gzip_comp_level 3;
#进行压缩的文件类型
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
#跟Squid等缓存服务有关,on的话会在Header里增加"Vary: Accept-Encoding"
gzip_vary on;
#IE6及以下浏览器关闭压缩
gzip_disable "MSIE [1-6]\.";
#缓冲区代理缓冲用户端请求的最大字节数,可以理解为保存到本地再传给用户
client_max_body_size 50m;
client_body_buffer_size 256k;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
#nginx跟后端服务器连接超时时间(代理连接超时)
proxy_connect_timeout 300s;
#连接成功后,后端服务器响应时间(代理接收超时)
proxy_read_timeout 300s;
proxy_send_timeout 300s;
#设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffer_size 64k;
#proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
proxy_buffers 4 32k;
#高负荷下缓冲大小(proxy_buffers*2)
proxy_busy_buffers_size 64k;
#设定缓存文件夹大小,大于这个值,将从upstream服务器传递请求,而不缓冲到磁盘
proxy_temp_file_write_size 64k;
#不允许代理端主动关闭连接
proxy_ignore_client_abort on;
server {
listen 8080;
server_name localhost;
charset urf-8;
# listen 9443 ssl;
# ssl_certificate /usr/local/nginx/conf/saenginx.crt;
# ssl_certificate_key /usr/local/nginx/conf/saenginx.key;
# ssl_session_timeout 30m;
# ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
# ssl_prefer_server_ciphers on;
root html;
index index.html index.htm index.jsp index.php;
#access_log logs/host.access.log main;
#access_log "pipe:rollback logs/host.access_log interval=1d baknum=7 maxsize=2G" main;
#前端
location /web/ {
root /usr/share/nginx/html;
#Angular专属配置
try_files $uri $uri/ /index.html = 404;
# try_files $uri $uri/ /index.html;
}
#后台
location /imcp/api/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_pass http://xxx.xxx.xxx.xxx:8080/;
}
# location / {
# proxy_redirect off;
# proxy_set_header Host $host;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_pass http://127.0.0.1/;
# }
#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;
}
# 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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}