KK91945564
Nginx的反向代理
虛擬主機
一個server{} 就是一個虛擬主機
基於域名的
Nginx方向代理示例(代理Tomcat):
安裝Tomcat
tar -zxvf apache-tomcat-7.0.61.tar.gz
解壓Tomcat
cd apache-tomcat-7.0.61
bin/startup.sh
(關閉bin/shutdown.sh)
訪問端口8080
http://192.168.17.9:8080/
通過訪問Nginx來訪問Tomcat
修改nginx.conf
server {
server_name www.nginx1.com
location / {
proxy pass http://192.168.17.9:8080/
}
}
此時,訪問Nginx
http://www.nginx1.com
負載均衡的方向代理
反向代理配置nginx.conf:
upstream 名字{
server IP:PORT;
server IP:PORT;
}
server {
location / {
proxy_passhttp://名字;
}
}
示例:
修改配置文件:
vim nginx.conf
upstream nginx {
sever 192.168.17.9:8080
sever 192.168.17.10:8080
}
server {
location / {
proxy_passhttp://nginx;
}
}
啟動2個Tomcat(在192.168.17.9和192.168.17.10兩臺機器上)
為了區分兩個Tomcat,修改index.jsp
vim webapps/ROOT/index.jsp
(幹掉
,因為裡面有個tomcat.css資源文件,每次都要去請求這個文件,達不到輪詢的效果)bin/startup.sh
service nginx reload
http://www.nginx1.com/
此時可以看到它輪詢訪問Tomcat(刷新就可以看到,因為兩個Tomcat的index.jsp已經做了區分)。
————————————————
小玖說科技
#負責壓縮數據流
gzip on;
gzip_min_length 1000;
gzip_types text/plain text/css application/x-javascript;
#設定負載均衡的服務器列表
#weigth參數表示權值,權值越高被分配到的幾率越大
upstream hello{
server 192.168.68.43:8080 weight=1;
server 192.168.68.45:8080 weight=1;
}
server {
#偵聽的80端口
listen 80;
server_name localhost;
#設定查看Nginx狀態的地址
location /nginxstatus{
stub_status on;
access_log on;
auth_basic "nginxstatus";
auth_basic_user_file htpasswd;
}
#匹配以jsp結尾的,tomcat的網頁文件是以jsp結尾
location / {
index index.jsp;
proxy_pass http://hello; #在這裡設置一個代理,和upstream的名字一樣
}
}
Nginx可以支持負載均衡,使網站獲得更好的性能和穩定性。