docker安装nginx
一、此方式只用于快速搭建使用
第一步 pull nginx
命令:docker pull nginx
第二步 启动nginx
命令:docker run --name nginx -p 80:80 -d nginx
第三步 查看成果
1)命令查看是否启动,命令:docker ps
2)网页访问,浏览器输入IP地址回车,就可以看到 “Welcome to nginx!”
二、附加项:
原因:虽然咱们能正常启动nginx,但配置得在容器中进行,这样的话太麻烦了,所以把配置文件给映射出来,方便配置与管理
第一步 在宿主机中创建挂载目录
mkdir -p /data/nginx/{conf,conf.d,html,log}
第二步 编写nginx.conf配置文件并放在conf文件夹中
nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
server{
listen 80;
server_name 175.24.235.187; #你的serverName
root /usr/share/nginx/html;
index index.html;
}
}
第三步 启动nginx容器
sudo docker run --name nginx -d -p 80:80 -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/log:/var/log/nginx -v /data/nginx/html:/usr/share/nginx/html -v /data/files:/files nginx
说明:
第一个-v:挂载nginx的主配置文件,以方便在宿主机上直接修改容器的配置文件
第二个-v:挂载容器内nginx的日志,容器运行起来之后,可以直接在宿主机的这个目录中查看nginx日志
第三个-v:挂载静态页面目录
第四个-v:挂载文件数据目录
注意:进入容器,删除conf.d文件夹下的default.conf;否则配置无效。
第四步 进入容器,删除conf.d文件夹下的default.conf
1)
docker exec -it nginx /bin/bash
2)
cd /etc/nginx/conf.d/
3)
rm -rf default.conf
4)
exit
5)
docker restart nginx
第五步 验证(修改宿主机挂载目录中的配置文件以重启nginx容器)
把server模块中的index改成main.html页面,以证明可以在宿主机中修改配置文件。(创建好main.html并上传到/data/nginx/html下)
server{
listen 80;
server_name your_server_name; #你的serverName
root /usr/share/nginx/html;
index main.html;
}
然后重启nginx容器:docker restart containerID
之后刷新页面你会发现页面从index.html变成了main.html
第六步 到此部署就完成了!!!
配置相关:
1、在location 中 echo “hello Nginx!” 访问可以直接输出文字
例:
location / {
echo "hello Nginx!"
}
2、location匹配规则:
1)最低级别匹配规则:
location / {
echo "hello Nginx!"
}
2)*别匹配规则:
location /user {
echo "hello user.hmtl"
}
3)其它级别匹配规则:
location ^~ /user {
echo "hello user.hmtl"
}
location ~^ /user {
echo "hello user.hmtl"
}
location ~ ^/[a-z] {
echo "hello user.hmtl"
}
location ~ ^/\a {
echo "hello user.hmtl"
}
3、反向代理细节:
location /user {
proxy_pass http://ip;
}
location /order/ {
proxy_pass http://ip/;
}
访问结果:
http://ip/user/xx...
http://ip/xx...
4、负载均衡配置
upstream order {
server 192.168.5.18:8080 weight=1;
server 192.168.5.18:8081 weight=1;
}
server{
location /order/ {
proxy_pass http://order/;
}
}
注:weight=1,配置的为权重,值越高权重越高
5、nginx搭建文件服务器
server{
listen 80;
server_name localhost,175.24.235.187; #你的serverName
client_max_body_size 1000M;
charset UTF-8;
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization' always;
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
location / {
root /usr/share/nginx/html;
index index.html;
}
#静态文件资源服务
location /files {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
alias /files;
allow all;
autoindex on;
}
}