概述
本篇文章主要先容apache和nginx的相关设置,tomcat的相关安装设置我在前面有写过一篇,具体先容通过两种设置要领设置nginx。
apache设置
源码安装
./configure --prefix=/usr/local/apache (安装目次)
make
make install
对付2.4以上版本的apache在举办源码安装的时候有的呆板会提示缺少部门插件譬喻:apr、apr-util、pcre,需要先将这些插件安装好然后再安装apache
YUM安装
yum install httpd
进入安装目次cnf.d文件夹下面,建设一个.conf后缀的文件
touch apa.conf
vim apa.cnf
Listen 8051
<VirtualHost *:8051>
ServerAdmin [email protected]
ServerName localhost
ErrorLog '/etc/httpd/logs/app_error.log'
CustomLog '/etc/httpd/logs/app_access.log' common
ProxyPass / balancer://cluster/ stickysession=JSESSIONID|jsessionid nofailover=On lbmethod=byrequests timeout=5 maxattempts=3
ProxyPassReverse / balancer://cluster/
ProxyRequests Off
ProxyPreserveHost On
<proxy balancer://cluster>
#BalancerMember ajp://localhost:8009 route=tomcat_a
BalancerMember http://localhost:8080/Front
#BalancerMember ajp://localhost:8010 route=tomcat_b
BalancerMember http://localhost:8081/Front
</proxy>
</VirtualHost>
设置文件一开始设置了apache的端口8051,然后在最下面设置了毗连tomcat的项目端口,我这里的设置的apache和tomcat都在一台处事器上面别离利用了差异的端口,在tomcat的webapps路径下面建设了一个Front项目,项目下面存放了一个test.jsp的测试页面
cd /usr/local/tomcat1/webapps
mkdir Front
cd Front
touch test.jsp
vim test.jsp
<font color=red>testa</font><b>
同样在tomcat2中也利用同样的要领建设测试页面,可是将testa改成testb
接下来确保8051端口被启用,也可以封锁防火墙,tomcat1、tomcat2都已启动,启动要领参考前面我写的关于tomcat的文章,确保httpd也以启动,假如已经将httpd插手了启动处事,
启动http处事
service httpd start
查察处事启动状态
service httpd status
接下来在欣赏器中输入:http://localhost:8051/test.jsp
假如刷新毗连页面功效是在testa和testb之间切换,说明设置乐成。
nginx设置
源码安装
建设nginx用户组
groupadd nginx
建设nginx用户
useradd -g nginx -s /sbin/nologin nginx
安装相关插件
yum install –y make zlib-devel openssl-devel pcre-devel
解压nginx安装包
tar zxvf nginx-1.8.0.tar.gz
进入安装包
cd nginx-1.8.0
安装
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module
make && make install
单个文件设置要领
建设测试页面
cd /usr/local/tomcat1/webapps
mkdir MFront
cd MFront
touch index.jsp
vim index.jsp
<font color=red>MFronttesta</font><b>
tomcat2也同样操纵,将MFronttesta改成MFronttestb
设置文件
cd /usr/local/nginx/conf
vim nginx.conf
user nginx nginx;
worker_processes 8;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 102400;
events
{
use epoll;
worker_connections 102400;
}
http
{
include mime.types;
default_type application/octet-stream;
fastcgi_intercept_errors on;
charset utf-8;
server_names_hash_bucket_size 512;
client_header_buffer_size 1024k;
large_client_header_buffers 4 128k;
client_max_body_size 300m;
sendfile on;
tcp_nopush on;
keepalive_timeout 600;
tcp_nodelay on;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 600;
proxy_send_timeout 50;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
###2012-12-19 change nginx logs
log_format main '$http_x_forwarded_for - $remote_user [$time_local] '$request' '
'$status $body_bytes_sent '$http_referer' '
''$http_user_agent' $request_time $remote_addr';
upstream Front {
server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s;
server 127.0.0.1:8081 weight=1 max_fails=2 fail_timeout=30s;
}
upstream MFront {
server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s;
server 127.0.0.1:8081 weight=1 max_fails=2 fail_timeout=30s;
}
####chinaapp.sinaapp.com
server {
listen 80;
server_name localhost;
location /Front
{
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://Front;
expires 3d;
}
location /MFront
{
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://MFront;
expires 3d;
}
}
}