隨着用戶量的增大,單臺服務器已經知足不了用戶的需求。php
準備工做:安裝 gcc、pcre-devel、zlib、OpenSSL 一下是在線 離線請戳這裏html
gcc 安裝
安裝 nginx 須要先將官網下載的源碼進行編譯,編譯依賴 gcc 環境,若是沒有 gcc 環境,則須要安裝:java
yum install gcc-c++
PCRE pcre-devel 安裝
PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx 的 http 模塊使用 pcre 來解析正則表達式,因此須要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發的一個二次開發庫。nginx也須要此庫。命令:linux
yum install -y pcre pcre-devel
zlib
zlib 庫提供了不少種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip ,因此須要在 Centos 上安裝 zlib 庫。nginx
yum install -y zlib zlib-devel
OpenSSL
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、經常使用的密鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程序供測試或其它目的使用。
nginx 不只支持 http 協議,還支持 https(即在ssl協議上傳輸http),因此須要在 Centos 安裝 OpenSSL 庫。c++
yum install -y openssl openssl-devel
Nginx 下載地址 正則表達式
一、解壓 nginx-1.8.1.tar.gz算法
tar -zxvf nginx-1.8.1.tar.gz cd nginx-1.8.1
二、編譯、安裝nginx後端
./configure make make install
三、啓動 nginxtomcat
[root@localhost nginx-1.8.1]# whereis nginx nginx: /usr/local/nginx [root@localhost nginx-1.8.1]# cd /usr/local/nginx/ [root@localhost nginx]# ./sbin/nginx #啓動
四、能夠去訪問ip nginx 端口默認是80 看到一下界面就表明成功了一半 PS:若是訪問不到,能夠試着把防火牆關掉。建議是 開啓80 端口
五、準備兩個tomcat 固然 你能夠在同一臺服務器上,而後改爲不痛端口。我這邊是在兩臺服務器上。一臺IP:10.13.5.11 另外一臺IP:10.13.5.12。爲了方便區別是兩個不一樣的tomcat 我把index.jsp 修改爲如下:
第一臺IP:10.13.5.11
第二臺IP:10.13.5.12
六、配置 nginx configure
[root@localhost nginx]# cd /usr/local/nginx/conf/
a、配置後端服務器組
upstream testsite.com{ server 10.13.5.11:8080 weight=1; server 10.13.5.12:8080 weight=2; }
b、使用服務器組 注意!!!!! 若是 location / { proxy_pass http://testsite.com; } proxy_pass http://testsite.com 末尾能夠不用加 / ,可是 location /other_string/ { proxy_pass http://testsite.com/; } proxy_pass http://testsite.com 以後必定要加 /
location /mysite/ { proxy_pass http://testsite.com/; proxy_redirect default; }
c、最後的全部代碼
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream testsite.com{ server 10.13.5.11:8080 weight=1; server 10.13.5.12:8080 weight=2; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } location /mysite/ { proxy_pass http://testsite.com/; proxy_redirect default; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
七、重啓nginx
[root@localhost conf]# ./sbin/nginx -s reload
八、成功
第一張訪問 IP:10.13.5.12 的服務器
第二張是訪問IP:10.13.5.11 的服務器