docker下安装并使用nginx
1:下载nginx
docker pull nginx
2:制作nginx镜像
docker run -it -d --name mynginx -p 80:80 -v /etc/nginx/nginx.conf:/etc/nginx/nginx.conf -v /home/img:/home/img nginx
run:启动容器
-i t d:后台启动一个终端
--name:容器的名称
-p:宿主机端口:容器端口
-v:宿主机目录:容器目录(会把宿主机器目录共享到容器内)
####以下为nginx配置:
/***
如果不想用这个配置可以先 docker -itd --name mynginx nginx 启动一个容器
拷贝容器内的镜像模板 到宿主机上
docer cp mynginx:/etc/nginx/nginx.conf /etc/nginx
#进入容器内
docker exec -it mynginx /bin/bash
#查看docker nginx容器默认配置(server) 复制到自己宿主机/etc/nginx/nginx.conf 配置中即可
cat /etc/nginx/conf.d/default.conf
**/
---------------------------nginx---------------------------------------------------------
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;
server {
listen 80;
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;
}
location /image {
alias /home/img;
autoindex on;
}
#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;
#}
}
}
3:测试
wget wget https://pic4.zhimg.com/v2-d2aee61cf9963be67c4ea8274b2601db_540x450.jpeg 下载图片
cp 1.jpeg /home/img
浏览器访问测试
http://192.168.124.16/image/11.jpeg
备注:
启动其他端口的容器,更改配置文件监听端口 和 启动命令即可:
[root@master photo]# cat /etc/nginx/nginx1.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;
server {
listen 6666;
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;
}
location /photo {
alias /home/photo;
autoindex on;
}
#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;
#}
}
}
#命令 注意 谷歌浏览器占用6666端口 其他浏览器测试
docker run -itd --name nginx1 -p 6666:6666 -v /etc/nginx/nginx1.conf:/etc/nginx/nginx.conf -v /home/photo/:/home/photo nginx
docker stop nginx1: 停止服务
docker rm nginx1:启动服务
docker logs nginx1:查看日志