Nginx 入门
应用场景
安装Nginx
搜索nginx镜像
docker search nginx
拉取nginx镜像
docker pull nginx
创建容器,设置端⼝映射、⽬录映射
# 在/root⽬录下创建nginx⽬录⽤于存储nginx数据信息
mkdir ~/nginx
cd ~/nginx
mkdir conf
cd conf
# 在~/nginx/conf/下创建nginx.conf⽂件,粘贴下⾯内容
vim 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;
}
~/nginx/conf.d/80.conf
server {
listen 80; # 监听的端⼝
server_name localhost; # 域名或ip
location / { # 访问路径配置
root /usr/share/nginx/html;# 根⽬录
index index.html index.htm; # 默认⾸⻚
}
error_page 500 502 503 504 /50x.html; # 错误⻚⾯
location = /50x.html {
root html;
}
}
docker run -id --name=c_nginx \
-p 9090:80 \
-p 9091:81 \
-p 9092:82 \
-v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf \
-v $PWD/conf.d:/etc/nginx/conf.d \
-v $PWD/logs:/var/log/nginx \
-v $PWD/html:/usr/share/nginx/html \
nginx
Nginx静态⽹站部署
静态⽹站的部署
将/资料/静态⻚⾯/index⽬录下的所有内容 上传到服务器的/usr/local/nginx/html下即可访问
配置虚拟主机
虚拟主机,也叫“⽹站空间”,就是把⼀台运⾏在互联⽹上的物理服务器划分成多个“虚拟”服务器。虚拟主机技术极⼤的促进了⽹络技术的应⽤和普及。同时虚拟主机的租⽤服务也成了⽹络时代的⼀种新型经济形式。
端⼝绑定
新建/usr/share/nginx/index/index.html,并拷贝到容器
hello nginx index 9091
docker cp /usr/share/nginx/index c_nginx:/usr/share/nginx/index
新建/usr/share/nginx/regist/regist.html,并拷贝到容器
hello nginx regist 9092
docker cp /usr/share/nginx/regist c_nginx:/usr/share/nginx/regist
修改Nginx 的配置⽂件:~/nginx/config.d/port.conf
server {
listen 81; # 监听的端⼝
server_name localhost; # 域名或ip
location / { # 访问路径配置
root /usr/share/nginx/index;# 根⽬录
index index.html index.htm; # 默认⾸⻚
}
error_page 500 502 503 504 /50x.html; # 错误⻚⾯
location = /50x.html {
root html;
}
}
server {
listen 82; # 监听的端⼝
server_name localhost; # 域名或ip
location / { # 访问路径配置
root /usr/share/nginx/regist;# 根⽬录
index regist.html; # 默认⾸⻚
}
error_page 500 502 503 504 /50x.html; # 错误⻚⾯
location = /50x.html {
root html;
}
}
域名绑定
顶级域名
⼆级域名
三级域名
域名与IP绑定
修改C:\Windows\System32\drivers\etc\hosts文件,添加
192.168.238.129 www.aaa.com
192.168.238.129 www.bbb.com
添加配置文件domain.conf,并添加相应文件到容器
server {
listen 80;
server_name www.xzk.com;
location / {
root /usr/share/nginx/cart;
index cart.html;
}
}
server {
listen 80;
server_name regist.xzk.com;
location / {
root /usr/share/nginx/search;
index search.html;
}
}
Nginx反向代理与负载均衡
反向代理
准备反向代理
搜索tomcat镜像
docker search tomcat
** 拉取tomcat镜像**
docker pull tomcat
创建容器,设置端⼝映射、⽬录映射
# 在/root⽬录下创建tomcat⽬录⽤于存储tomcat数据信息
mkdir ~/tomcat
cd ~/tomcat
docker run -id --name=c_tomcat \
-p 8080:8080 \
-v $PWD:/usr/local/tomcat/webapps \
tomcat
配置反向代理
~/nginx/config.d/proxy.conf
upstream tomcat-aaa{
server 192.168.238.129:8080;
}
server {
listen 80; # 监听的端⼝
server_name www.aaa.com; # 域名或ip
location / { # 访问路径配置
# root index;# 根⽬录
proxy_pass http://tomcat-aaa;
index index.html index.htm; # 默认⾸⻚
}
}
负载均衡
准备负载均衡
配置负载均衡
修改 Nginx配置⽂件:lb.conf
upstream tomcat-huike {
server 192.168.238.129:8080;
# 出现次数是其它服务器的2倍
server 192.168.238.129:8081 weight=2;;
server 192.168.238.129:8082;
}
server {
listen 80; # 监听的端⼝
server_name www.huike.com; # 域名或ip
location / { # 访问路径配置
# root index;# 根⽬录
proxy_pass http://tomcat-huike;
index index.html index.htm; # 默认⾸⻚
}
error_page 500 502 503 504 /50x.html; # 错误⻚⾯
location = /50x.html {
root html;
}
}