服務器IP是192.168.36.136html
一、直接yum install nginx便可python
二、主配置文件是/etc/nginx/下的nginx.conf,另一個是/etc/nginx/conf.d/下的default.confnginx
主配置文件最末行經過 include /etc/nginx/conf.d/*.conf;引入apache
三、service nginx start啓動後,訪問http://192.168.36.136/會出現Nginx的默認首頁centos
默認首頁配置要看default.conf裏面的server緩存
listen 80;#監聽端口,若是換成81,那麼訪問就是http://192.168.36.136:81/
server_name localhost;#監聽地址,nginx服務器地址tomcat
#下面就是根據location路由規則找到默認頁面的,若是index.html不存在會找index.htm;對於詳細若有規則可參考Nginx Location配置總結服務器
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}負載均衡
四、修改了配置文件後能夠經過nginx -t命令檢查一下,nginx -s reload從新加載配置便可生效,若是還不行就用service命令重啓服務(只是簡單學習,因此沒有研究緩存配置問題和靜態文件配置)jsp
五、如今在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
六、再次訪問http://192.168.36.136/test/頁面顯示以下
頁面顯示這樣,查看下面報錯,沒有引入圖片和CSS靜態文件,這種錯就是配置的時候根路徑是/test/,後面真正用的時候就直接寫項目根路徑便可
七、上面只是用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/