nginx安装 及 tomcat负载均衡
一、Nginx安装
1、下载nginx
2、解压
tar -zxvf nginx-1.9.9.tar.gz
3、安装依赖包
yum -y install pcre-devel
yum -y install openssl openssl-devel
3、选择安装目录
./configure--prefix=xxx
3、编译、安装
进入解决后的nginx主目录,分别运行make、make install 命令进行编译、安装。
make 编译 (make的过程是把各种语言写的源码文件,变成可执行文件和各种库文件)
make install 安装 (make install是把这些编译出来的可执行文件和库文件复制到合适的地方)需要root权限)
一般来说./configure不指定目录安装的话是安装在/usr/local/nginx
用编译模式安装软件可以用 ./configure --prefix=xxx来指定安装目录,默认安装在
4、检查安装结果
进入目录:cd /usr/local/nginx/sbin
运行测试:./nginx -t
启动:./nginx
关闭:./nginx -s stop
查看帮忙命令 ./nginx-h
访问http://localhost 如果显示正常,则表示安装成功。 访问的页面存放在 nginx的html目录下。也可以修改配置文件指到其它位置;
二、nginx负载均衡配置(红色部分为增加的配置,其他不变)
#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 www.zhangzhi.com{
server 192.168.187.128:8080 max_fails=3 weight=1 fail_timeout=60s;
server 192.168.187.129:8080 max_fails=3 weight=1 fail_timeout=60s;
}
server {
listen 80;
server_name www.zhangzhi.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://www.zhangzhi.com;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
#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;
# }
#}