隨着用戶量的增大,單台服務器已經滿足不了用戶的需求。
准備工作:安裝 gcc、pcre-devel、zlib、OpenSSL 一下是在線 離線請戳這里
gcc 安裝
安裝 nginx 需要先將官網下載的源碼進行編譯,編譯依賴 gcc 環境,如果沒有 gcc 環境,則需要安裝:
yum install gcc-c++
PCRE pcre-devel 安裝
PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx 的 http 模塊使用 pcre 來解析正則表達式,所以需要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發的一個二次開發庫。nginx也需要此庫。命令:
yum install -y pcre pcre-devel
zlib
zlib 庫提供了很多種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip ,所以需要在 Centos 上安裝 zlib 庫。
yum install -y zlib zlib-devel
OpenSSL
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程序供測試或其它目的使用。
nginx 不僅支持 http 協議,還支持 https(即在ssl協議上傳輸http),所以需要在 Centos 安裝 OpenSSL 庫。
yum install -y openssl openssl-devel
Nginx 下載地址
1、解壓 nginx-1.8.1.tar.gz
tar -zxvf nginx-1.8.1.tar.gz cd nginx-1.8.1
2、編譯、安裝nginx
./configure make make install
3、啟動 nginx
[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 #啟動
4、可以去訪問ip nginx 端口默認是80 看到一下界面就代表成功了一半 PS:如果訪問不到,可以試着把防火牆關掉。建議是 開啟80 端口

5、准備兩個tomcat 當然 你可以在同一台服務器上,然后改成不痛端口。我這邊是在兩台服務器上。一台IP:10.13.5.11 另一台IP:10.13.5.12。為了方便區別是兩個不同的tomcat 我把index.jsp 修改成以下:
第一台IP:10.13.5.11

第二台IP:10.13.5.12

6、配置 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;
# }
#}
}
7、重啟nginx
[root@localhost conf]# ./sbin/nginx -s reload
8、成功
第一張訪問 IP:10.13.5.12 的服務器

第二張是訪問IP:10.13.5.11 的服務器

