# ngnix
创建docker下的nginx:
docker run -id --name=c_nginx \
-p 80:80 \
-p 81:81 \
-p 82:82 \
-v /root/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /root/nginx/conf.d:/etc/nginx/conf.d \
-v /root/nginx/logs:/var/log/nginx \
-v /root/nginx/html:/usr/share/nginx/html \
nginx
在/root/nginx中存储相应的ngnix配置信息:
容器中的文件目录和宿主机的文件目录对比:
docker文件目录 | 宿主机 文件目录 | 备注 |
---|---|---|
~/nginx/conf/ | /etc/nginx/nginx.conf | 主配置文件 |
~/nginx/conf.d | /etc/nginx/conf.d | 配置文件存放文件夹 |
~/nginx/log | /var/log/nginx | 日志文件夹 |
~/nginx/html | /usr/share/nginx/html | 静态资源文件夹 |
1.静态资源部署:
将静态资源放置~/nginx/html目录中,即可访问.
2.端口绑定:
vim ~/nginx/conf.d/static.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;
}
}
3.域名绑定:
server {
listen 81; # 监听的端⼝
server_name www.ahunag.com; # 域名或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;
}
}
4.反向代理
upstream tomcat-kkb{
server 192.168.220.12:8080;
}
server {
listen 80; # 监听的端⼝
server_name www.kkb.com; # 域名或ip
location / { # 访问路径配置
# root index;# 根⽬录
proxy_pass http://tomcat-kkb;
index index.html index.htm; # 默认⾸⻚
}
}
5.负载均衡:(默认为轮训)设置权重
upstream tomcat-kkb{
server 192.168.220.12:8888 weight=2;
server 192.168.220.12:8080;
}
6.解决前端AJax跨域问题
ajax请求:url: “/test”
使用代理到->192.168.111.135:8888/test
server{
listen 81;
location / {
root /usr/share/nginx;
index test.html;
}
location /test {
proxy_pass http://192.168.111.135:8888/test;
}
}