Nginx的使用
Nginx的使用
1.http块
1.1.server
server {
listen 80;#监听端口
server_name localhost;#监听ip
2.反向代理示例一
使用 nginx 反向代理,访问 www.666.com 直接跳转到 127.0.0.1:8080
server {
listen 80;
server_name www.666.com;
location / {
proxy_pass http://127.0.0.1:8080;
index index.html index.htm index.jsp;#指定网站初始页
}
}
3. 反向代理示例二
使用 nginx 反向代理,根据访问的路径跳转到不同端口的服务中 nginx 监听端口为 9001,
访问 http://127.0.0.1:9001/edu/ 直接跳转到 127.0.0.1:8081
访问 http://127.0.0.1:9001/vod/ 直接跳转到 127.0.0.1:8081
server {
listen 9001;
server_name localhost;
location ~ /edu/ {
proxy_pass http://127.0.0.1:8081;
}
location ~ /vod/ {
proxy_pass http://127.0.0.1:8082;
}
}
- ~:用于表示 uri 包含正则表达式,并且区分大小写。
- ~*:用于表示 uri 包含正则表达式,并且不区分大小写。
4.负载均衡
http{
...
upstream server_pool{
ip_hash;
server 192.168.5.21:80 weight=10; #权重
server 192.168.5.22:80 weight=10;
}
server {
listen 80;
server_name www.666.com;
location / {
proxy_pass http://server_pool;
}
}
}
5.动静分离
server {
listen 80;
server_name www.666.com;
location /www/ {
root /data/; #指定资源的位置
index index.html index.htm index.jsp;
}
location /image/ {
root /data/;
autoindex on;
}
}