一、Nginx upstream
Ngx_http_upstream_module模块可实现七层负载均衡,定义的服务器组可被proxy_pass、fastcgi_pass、uwsgi_pass、scgi_pass和memcached_pass所引用。
1、实验场景
Nginx upstream IP:192.168.0.110
apache node1 IP:192.168.0.40
apache node2 IP:192.168.0.30
2、Nginx upstream配置(结合proxy为例)
1.在http区段定义服务器组[root@Nginx conf]# grep -Ev "#|^$" nginx.conf
worker_processes 1;
worker_rlimit_nofile 65535;
events {
worker_connections 20480;
}
http {
include mime.types;
default_type application/octet-stream;
upstream nodeserver{
server 192.168.0.30;
server 192.168.0.40;
}
sendfile on;
keepalive_timeout 65;
include server.conf;
}
2.在location区段的proxy_pass中引用服务器组
[root@Nginx conf]# grep -Ev "#|^$" server.conf
server {
listen 80;
server_name localhost;
location / {
root html/xn1;
index index.html;
}
location /user {
proxy_pass http://nodeserver;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
注意:在upstream定义的服务器组中,server可配置多种形式的服务器,在proxy_pass中引用服务器
填入服务器组名即可。
3、测试
4、主要附加参数说明
server 192.168.0.30 weight=2;此处的weight为权重,默认为1;
主要算法,rr,wrr,ip_hash等,可在upstream中定义;
server 192.168.0.30 max_files=3 fail_timeout=30s;此处的max_files,fail_timeout为健康状态检测,超过3次未响应,超时30s,就从server列表中移除。
server 192.168.0.30 backup;标记为备份
server 192.168.0.30 down;标记为不可用,与ip_hash算法一同使用
更多用法,比如基于sticky的三种绑定方法等详见官方站点:
http://nginx.org/en/docs/http/ngx_http_upstream_module.html
二、Nginx fastcgi
1、fastcgi全称为高速的通用网关接口,是HTTP服务器与动态脚本语言之间通信的接口,其工作模式与反向代理差不多,实现客户端请求的动静分离。
2、LNMP简单安装
PHP主机配置1.安装php-fpm[root@Nginx conf]# yum install php-fpm2.如果php与nginx不在同一台主机,则修改/etc/php-fpm.conf中的监听地址,我这里在一起,不用修改3.启动php[root@Nginx conf]# service php-fpm startNginx主机配置1.Nginx配置,启用了默认的PHP配置[root@Nginx conf]# grep -Ev "#|^$" server.conf server { listen 80; server_name localhost;add_header X-Via $server_addr; location / { root html/xn1; index index.html;} location /user { proxy_pass http://nodeserver; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; add_header X-Cache $upstream_cache_status; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } 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; } }2.配置fastcgi_params[root@Nginx conf]# grep -Ev "#|^$" fastcgi_paramsfastcgi_param GATEWAY_INTERFACE CGI/1.1;fastcgi_param SERVER_SOFTWARE nginx;fastcgi_param QUERY_STRING $query_string;fastcgi_param REQUEST_METHOD $request_method;fastcgi_param CONTENT_TYPE $content_type;fastcgi_param CONTENT_LENGTH $content_length;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;fastcgi_param SCRIPT_NAME $fastcgi_script_name;fastcgi_param REQUEST_URI $request_uri;fastcgi_param DOCUMENT_URI $document_uri;fastcgi_param DOCUMENT_ROOT $document_root;fastcgi_param SERVER_PROTOCOL $server_protocol;fastcgi_param REMOTE_ADDR $remote_addr;fastcgi_param REMOTE_PORT $remote_port;fastcgi_param SERVER_ADDR $server_addr;fastcgi_param SERVER_PORT $server_port;fastcgi_param SERVER_NAME $server_name;3.重启nginx[root@Nginx conf]# nginx -s reload4.给出php页面[root@Nginx html]# vim index.php<?php phpinfo();?>
3、测试
4、安装php-mysql驱动mysql(都在本机,不在同一机器更改监听端口即可)
1.安装php-mysql[root@Nginx conf]# yum install php-mysql2.安装mysql[root@Nginx conf]# yum install mysql-server3.重启php,启动mysql[root@Nginx conf]# service php-fpm restart停止 php-fpm: [确定]正在启动 php-fpm: [确定][root@Nginx conf]# service mysqld start初始化 MySQL 数据库: Installing MySQL system tables...OKFilling help tables...OKTo start mysqld at boot time you have to copysupport-files/mysql.server to the right place for your systemPLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !To do so, start the server, then issue the following commands:/usr/bin/mysqladmin -u root password 'new-password'/usr/bin/mysqladmin -u root -h Nginx password 'new-password'Alternatively you can run:/usr/bin/mysql_secure_installationwhich will also give you the option of removing the testdatabases and anonymous user created by default. This isstrongly recommended for production servers.See the manual for more instructions.You can start the MySQL daemon with:cd /usr ; /usr/bin/mysqld_safe &You can test the MySQL daemon with mysql-test-run.plcd /usr/mysql-test ; perl mysql-test-run.plPlease report any problems with the /usr/bin/mysqlbug script! [确定]正在启动 mysqld: [确定]
5、测试LNMP连接
[root@Nginx conf]# vim /usr/local/nginx/html/index.php<?php $conn = mysql_connect('127.0.0.1','root',''); if ($conn) echo succ; else echo fail; mysql_close();?>
[root@Nginx conf]# service mysqld stop停止 mysqld: [确定]
更多fastcgi模块属性详见:http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html
本文出自 “linux启航” 博客,请务必保留此出处http://jiayimeng.blog.51cto.com/10604001/1895585