服務器IP是192.168.36.136
1、直接yum install nginx即可
2、主配置文件是/etc/nginx/下的nginx.conf,另外一個是/etc/nginx/conf.d/下的default.conf
主配置文件最末行通過 include /etc/nginx/conf.d/*.conf;引入
3、service nginx start啟動后,訪問http://192.168.36.136/會出現Nginx的默認首頁
默認首頁配置要看default.conf里面的server
listen 80;#監聽端口,如果換成81,那么訪問就是http://192.168.36.136:81/
server_name localhost;#監聽地址,nginx服務器地址
#下面就是根據location路由規則找到默認頁面的,如果index.html不存在會找index.htm;對於詳細如有規則可參考Nginx Location配置總結
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
4、修改了配置文件后可以通過nginx -t命令檢查一下,nginx -s reload重新加載配置即可生效,如果還不行就用service命令重啟服務(只是簡單學習,所以沒有研究緩存配置問題和靜態文件配置)
5、現在在192.168.36.134服務器上已經啟動了一個tomcat,並且外部測試可以訪問http://192.168.36.134:8080/,現在就想通過nginx訪問
為了保持之前的location,又添加一個如下
location /test/ {
proxy_pass http://192.168.36.134:8080/index.jsp;
}
配置好后,nginx -s reload重新加載配置,訪問http://192.168.36.136/test/報錯
這個錯誤頁面就是
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
配置下指向的頁面,也就是說,路由配置起作用了,只是nginx后台與apache服務器之間的問題,百度了centos7 502問題,這是紅帽和centos6.6版出現的問題,解決方案如下
yum -y install policycoreutils-python
cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginx
semodule -i mynginx.pp
6、再次訪問http://192.168.36.136/test/頁面顯示如下
頁面顯示這樣,查看下面報錯,沒有引入圖片和CSS靜態文件,這種錯就是配置的時候根路徑是/test/,后面真正用的時候就直接寫項目根路徑即可
7、上面只是用nginx配置了一台服務器,要配置多台實現負載均衡效果配置如下
在http下添加upstream(文件/etc/nginx/nginx.conf)
upstream hostname {
server 192.168.36.136:8080 weight=1;
server 192.168.36.134:8080 weight=10;
}
然后修改server下路由規則為/test/的location
location /test/ {
proxy_pass http://hostname/index.jsp;
}
如此后重新加載配置文件訪問http://192.168.36.136/test/