安装Nginx之前,必须解决依赖的问题,所以要首先安装上:pcre-x.x.x.tar.gz 和pcre-devel-x.x.x.rpm这两个包。
1.安装pcre-x.x.x.tar
tar zxvf pcre-x.x.x.tar.gz cd pcre-x.x.x ./configure make && make install
2.安装pcre-devel-x.x.x.rpmrpm -ivh pcre-devel-x.x.x.rpm
3.安装好依赖之后就安装nginx-x.x.x.tar.gztar zxvf nginx-x.x.x.tar.gz cd nginx-x.x.x ./configure --with-http_stub_status_module --prefix=/usr/local/nginx --with-debug --with-http_sub_module make && make install
这里要注意的是:编译的时候,nginx有很多模块可以编译的,但是对于一个用来进行转发的负载均衡nginx来说,以上两个足够了。安装好之后,nginx的默认安装路径是:/usr/local/nginx/
4.修改配置文件
vim /usr/local/nginx/conf/nginx.conf
要进行转发,就要把转发的服务器列表列出来;如下:http{ . . . upstream <span style="background-color: rgb(255, 102, 0); ">Servers</span> { server ip地址:端口 weight=10;(权值,表明转发的优先权,相等的时候就是均衡转发) server ip地址:端口 weight=10; } server{ . . location / { proxy_pass <span style="background-color: rgb(255, 102, 0); ">Servers</span>; root html; index index.html index.htm; } }}
最后做点小小的优化;a。查看一下服务器的cpu核心数
cat /proc/cpuinfo
然后再nginx的配置文件里面的开头处,加上:worker_processes 4;(这里取值可以是跟核心数一样,也可以是核心数的2倍,看需要)
b。接着在下面加上工作的连接数配置events { worker_connections 102400;(这里也是看情况修改) }
下面是完整的修改后的配置文件:#user nobody; worker_processes 4; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 102400; } 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 <span style="background-color: rgb(255, 153, 0); ">Servers </span>{ server ip地址:端口 weight=10; server ip地址:端口 weight=10; } server { listen 8080; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass <span style="background-color: rgb(255, 204, 0); ">Servers</span>; root html; index index.html index.htm; } #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 ; #} # 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; server { listen 8080; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass ; root html; index index.html index.htm; } #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 ; #} # 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; #} }
配置文件弄好之后,可以先测试一下是否配置正确:/usr/local/nginx/sbin/nginx -t
如果有错,会告诉你哪句有错的,很人性化。接着是启动: