一.Centos下安装Nginx
注:Nginx1.9.0以后才支持TCP负载均衡,而且在编译时需要加入--with stream参数才会安装此功能。
(1)在/usr/local下创建nginx目录:
[root@wlwddc usr]#mkdir /usr/local/nginx
安装依赖软件pcre-devel和libevent
[ root@wlwddc nginx]# yum install pcre-devel libevent openssl(2)安装配置nginx
[ root@wlwddc nginx]# tar -zxvf nginx-1.6.3.tar.gz -C /usr/local/src/
[ root@wlwddc nginx]# cd /usr/local/src/nginx-1.6.3/
[ root@wlwddc nginx-1.6.3]# ./configure --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --with-pcre
[ root@wlwddc nginx-1.6.3]# make && make install(3)为了能够启动服务因此要把控制脚本放到搜索路径去,因此要编辑/etc/profile文件
[ root@wlwddcnginx]# vim /etc/profile
加上PATH=$PATH:/usr/local/nginx/sbin
使配置生效:
[ root@wlwddc nginx]# ./etc/profile(4)解决一些错误
//检验语法错误
[ root@wlwddc~]# nginx -t
nginx: the configuration file/etc/nginx/nginx.conf syntax is ok
nginx: [emerg] getpwnam("nginx")failed
nginx: configurationfile /etc/nginx/nginx.conf test failed上面错误表明没有创建用户合组
//创建用户和组
[ root@wlwddc~]# groupadd -r nginx
[ root@wlwddc ~]# useradd -r -g nginx nginx
//由于编译过程中指明了/var/tmp/nginx/client/目录和/var/tmp/nginx/proxy/目录,因此要创建这两个目录。
[ root@wlwddc~]# mkdir -pv /var/tmp/nginx/client
[ root@wlwddc ~]# mkdir -pv /var/tmp/nginx/proxy
(5)启动nginx服务,并查看端口,
//进入目录
[ root@wlwddc ~]# cd /usr/local/nginx/sbin/
[ root@wlwddc sbin]# ./nginx
//出现错误,显示80口被占用:
[root@wlwddc sbin]# netstat -tupln |grep 80
tcp 0 0 :::80 :::* LISTEN 1907/httpd
//将端口修改为82
[root@wlwddc sbin]# cd /etc/nginx/
[root@wlwddc nginx]# vim nginx.conf
server {
listen 80;(-------此处修改为82)
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
(6) 重新执行测试是否正确
[root@wlwddc nginx]# cd /usr/local/nginx/sbin/
[root@wlwddcsbin]# ./nginx
[root@wlwddc sbin]# netstat -tupln |grep nginx
tcp 0 0 0.0.0.0:82 0.0.0.0:* LISTEN 22427/nginx(7) 最后还应该修改防火墙
[root@wlwddc sbin]# vi/etc/sysconfig/iptables
加入:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 82 -j ACCEPT
//重启防火墙
[root@wlwddc sbin]#service iptables restart//然后在浏览器端访问
http://localhost:82 出现nginx成功页面
二.Centos下安装Tomcat
见:http://blog.csdn.net/fred_yang2013/article/details/45394801
其他:
1. Centos设置Tomcat开机启动
(1)进入编辑
vi/etc/rc.d/rc.local
(2)打开后在最底部复制下面
export JDK_HOME=/usr/java/jdk1.6.0_45
export JAVA_HOME=/usr/java/jdk1.6.0_45
/usr/local/apache-tomcat/bin/startup.sh
(3)重启服务器,试试能不能访问。
Tips:如果JDK和Tomcat安装的目录不跟上面的一样,请修改相应路径
2. CentOS Tomcat修改默认端口
(1) 熟悉tomcat的配置文件
#/usr/local/apache-tomcat/conf/server.xml/编辑Tomcat配置文件
(假设我是安装在/usr/local/apache-tomcat/ 实际情况参考你服务器安装的路径为准)
打开配置文件里找到下面这一段
<Connector port="8080" protocol="HTTP/1.1"connection Timeout="20000" redirect Port="8443" />
(2) 修改端口
1、打开tomcat配置文件
#vi/usr/local/apache-tomcat/conf/server.xml
2、打开Tomcat配置文件之后按 /Connector port 快速检索可以修改端口号
3、修改端口,按 i 进行,将8080 修改为 8180
4、按:wq 保存并退出
(3) 重启Tomcat 并检测
启动Tomcat服务
在根目录下执行
[root@primary /]# find -name apache-tomcat
显示的就是安装路径
./usr/local/apache-tomcat
然后执行启动:
[root@primary /]# cd /usr/local/apache-tomcat/bin
[root@primary bin]# bash catalina.sh start
1、重启Tomcat服务
停用#/var/apache-tomcat/bin/shutdown.sh
(如果不行,#cd /var/apache-tomcat/bin/ 再执行#shutdown.sh)
启用 #bashcatalina.sh start /要先进入#/usr/local/apache-tomcat/bin/ 再操作
2、尝试访问
服务器IP 如http://192.168.100.110:8180 如果访问不了,检查防火墙或者有没有启动成功
若远程登录不了,进入server.xml文件,将如下的注释去掉:
<Connector port="8180" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"/>
<!-- A "Connector" using the shared thread pool-->
<Connector executor="tomcatThreadPool"
port="8180"protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"/>
<!-- Define a SSL HTTP/1.1 Connector on port 8443
This connector uses the JSSE configuration, when using APR, the
connector should be using the OpenSSL style configuration
described in the APR documentation -->
<!--
tomcat登录密码在tomcat-users.xml文件如下设置:
<role rolename="tomcat"/>
<rolerolename="manager-gui"/>
<role rolename="admin-gui"/>
<user username="wlwddc"password="111111" roles="manager-gui,admin-gui"/>
<!--
<user username="both"password="tomcat" roles="tomcat,role1"/>
<user username="role1"password="tomcat" roles="role1"/>
-->
</tomcat-users>
三.轮询方式负载均衡配置
1. http负载均衡配置
[root@wlwddc nginx]# cd /etc/nginx/
[root@wlwddc nginx]# ls
fastcgi.conf koi-win scgi_params
fastcgi.conf.default mime.types scgi_params.default
fastcgi_params mime.types.default uwsgi_params
fastcgi_params.default nginx.conf uwsgi_params.default
koi-utf nginx.conf.default win-utf修改nginx配置文件:
红色部分是比较重要的修改部分:
[root@wlwddcnginx]# vim nginx.conf
user root;
worker_processes 10;
worker_rlimit_nofile 100000;
error_log /var/log/nginx/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
events {
worker_connections 1024;
use epoll;
}
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
server_tokens off;
#keepalive_timeout 0;
gzip on;
gzip_static on;
gzip_comp_level 5;
gzip_min_length 1024;
keepalive_timeout 65;
limit_conn_zone $binary_remote_addr zone=addr:10m;
upstream tomcat-host{
server 192.168.100.164:8081 weight=10;
server 192.168.100.110:8180 weight=10;
}
server {
listen 82;
server_name 192.168.100.224;
#charset koi8-r;
#access_log logs/host.access.log main;
location /{
root html;
index index.html index.htm;
proxy_pass http://tomcat-host;
# 获取真实IP
proxy_set_header X-Real-IP $remote_addr;
# 获取代理者的真实IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# nginx非80端口处理
proxy_set_header Host $host:$server_port;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root html;
fastcgi_pass 192.168.100.224:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
}
重启nginx:
[root@wlwddc sbin]# ./nginx -s reload
打开Tomcat服务后在浏览器输入:http://192.168.100.224:82/ 就会跳到对应tomcat服务
2. TCP负载均衡设置
TCP负载均衡主要针对quickserver
首先将quickserver文件夹拷贝到目标主机,使用ssh复制命令:
scp -r /root/file_name 192.168.100.120://root
拷贝至目标主机后执行:
[root@localhost wlwddc_car]# ./wlwddc_car.sh
Loading QuickServer v2.1.0 [PID:7085]... Done
log4j:WARN No appenders could be found for logger (org.apache.commons.digester3.Digester.sax).
log4j:WARN Please initialize the log4j system properly.
-------------------------------
Name : WlwddcServer v 1.0
Port : 4455
-------------------------------
可以查看端口监听情况:
[root@localhost ~]# netstat -tulpn | grep :4455
tcp 0 0 0.0.0.0:4455 0.0.0.0:* LISTEN 7085/java
TCP负载均衡在nginx.conf中加入如下模块:
#TCP模块
stream {
upstream backend {
#hash $remote_addr consistent;//根据IP地址hash值分配服务器,往往一个IP只能访问固定的服务器
server 192.168.100.163:4455;
server 192.168.100.160:4455;
#server 127.0.0.1:12345 max_fails=3 fail_timeout=30s;
#server unix:/tmp/backend3;
}
server {
listen 4455;
proxy_connect_timeout 1s;
proxy_timeout 60s; #原始参数是3秒,测试时每隔30秒发一次消息挂掉了,改成60s后就好了
proxy_pass backend;
}
# server {
# listen [::1]:12345;
# proxy_pass unix:/tmp/stream.socket;
# }
}
防火墙开放4455端口
重启nginx:
[root@wlwddc sbin]# ./nginx -s reload
使用测试工具往服务器224发送TCP数据包,然后使用命令截取数据包,使用Wireshark分析是否有数据包传送:
tcpdump tcp port 4455 and host192.168.100.224 -w /home/224.cap
tcpdump tcp port 4455 and host192.168.100.160 -w /home/160.cap
tcpdump tcp port 4455 and host192.168.100.163 -w /home/163.cap
nginx可如下启动、重启和关闭
[root@wlwddc /]#cd /usr/local/nginx/sbin
[root@wlwddc sbin]# ./nginx
[root@wlwddc sbin]# ./nginx -s reload
[root@wlwddc sbin]# nginx -s stop
最后设置下nginx自启动,
vi/etc/rc.local
添加
/usr/local/nginx/sbin/nginx
可能出现的问题:
1. 当nginx端口不是默认的80端口时,访问会重定向至默认80端口导致错误
解决方法:http://shitouququ.blog.51cto.com/24569/1551221,我是使用该文中的第三种方法解决的,前两种方法没有效果,全文如下:
问题
location /app1/ {上面的代码只是简单举例,其中处理静态内容的部分也可以用目录alias或者root的方式去处理,效果应该一样的,但是具体区别我也没深入了解,不过这不是今天的重点。在这个配置下出现的问题就是当访问http://host:81/app1/Login.do的时候,登录成功需要301跳转到用户中心页面,然后跳转的地址本应该是http://host:81/app1/userindex.do,但是结果不太尽如人意,浏览器实际出现的地址http://host/app1/userindex.do。这里面的问题就是81端口没了,跑80端口去了,自然就404了。扯了一大段,这就是今天想说的问题。
index index.jsp index.html index.html index.shtml;
proxy_pass http://localhost:8080/app1/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}location ~* ^.+\.(png|jpg|jpeg|gif|ico|css|js|xml)$ {
root /home/gap/app/apache-tomcat-5.5.14/webapps;}
解决思路1:利用nginx的proxy_redirect
proxy_redirect http://host http://host:81;
解决思路2:找到问题原因,修改出错的地方解决
// FIXME: the code below doesnt belongs to here, // this is only have sense // in Http11, not in ajp13..// At this point the Host header has been processed.// Override if the proxyPort/proxyHost are set String proxyName = connector.getProxyName();int proxyPort = connector.getProxyPort();if (proxyPort != 0) {
req.setServerPort(proxyPort);}if (proxyName != null) {
req.serverName().setString(proxyName);}
解决思路3:如果是程序自动获取URl
总结
2. nginx: [emerg] getpwnam("nginx--group=nginx") failed
解决方法:
1. 在nginx.conf中 把#user nobdy改为user nginx
2. 在编译前修改下install文件或make文件,将新建用户,新建一些自定义目录都添加下,再编译生成rpm包就OK了。
3. 在nginx.conf中 把user nobody的注释去掉
其他: