Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。本文介绍如何在docker下部署nginx。
Nginx Docker拉取、配置、启动。 准备docker镜像拉取nginx docker
docker pull nginx docker images配置nginx 创建配置目录:
mkdir -p /opt/nginx/config/conf.d mkdir -p /opt/nginx/logs mkdir -p /opt/nginx/www创建配置
- 在/opt/nginx/config下创建 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;
}
- 在 /opt/nginx/config/conf.d 目录下创建 default.conf 配置:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#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;
}
# 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;
#}
}
启动
docker启动命令:
docker run
--name {nginxInstance}
-p 80:80
-v /opt/nginx/config/nginx.conf:/etc/nginx/nginx.conf
-v /opt/nginx/config/conf.d:/etc/nginx/conf.d
-v /opt/nginx/logs:/var/log/nginx
-v /opt/nginx/www:/usr/share/nginx
-d {镜像名:版本}
{nginxInstance} :Nginx实例名
{镜像名}:一般拉取最新镜像直接换成nginx就行
- 在 /opt/nginx/www/html 放入index.html, 否则汇报404
- 访问 http://localhost 会显示 index.html 页面
- 如果远程服务器,访问不了的话,检查telnet ip port 看看是否通



