Nginx学习日记第五篇 -- upstream及fastcgi

一、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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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、测试

wKiom1iYbHyR6CZ5AAAbwbP3m4A863.png-wh_50

wKioL1iYbIqyMbdjAAAXCYOoHMU764.png-wh_50


 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简单安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
PHP主机配置
1.安装php-fpm
[root@Nginx conf] # yum install php-fpm
2.如果php与nginx不在同一台主机,则修改 /etc/php-fpm .conf中的监听地址,我这里在一起,不用修改
3.启动php
[root@Nginx conf] # service php-fpm start
 
Nginx主机配置
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_params
fastcgi_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 reload
 
4.给出php页面
[root@Nginx html] # vim index.php
<?php
         phpinfo();
?>

 

 3、测试

wKiom1iZSBfhz3tQAACtJjQSFS8220.png-wh_50


 4、安装php-mysql驱动mysql(都在本机,不在同一机器更改监听端口即可)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
1.安装php-mysql
[root@Nginx conf] # yum install php-mysql
 
2.安装mysql
[root@Nginx conf] # yum install mysql-server
 
3.重启php,启动mysql
[root@Nginx conf] # service php-fpm restart
停止 php-fpm:                                             [确定]
正在启动 php-fpm:                                         [确定]
[root@Nginx conf] # service mysqld start
初始化 MySQL 数据库: Installing MySQL system tables...
OK
Filling help tables...
OK
 
To start mysqld at boot  time  you have to copy
support-files /mysql .server to the right place  for  your system
 
PLEASE 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_installation
 
which  will also give you the option of removing the  test
databases and anonymous user created by default.  This is
strongly 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.pl
cd  /usr/mysql-test  ; perl mysql- test -run.pl
 
Please report any problems with the  /usr/bin/mysqlbug  script!
 
                                                            [确定]
正在启动 mysqld:                                          [确定]

 

 5、测试LNMP连接

1
2
3
4
5
6
7
8
9
[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();
?>

 wKioL1iZTEzjrNTWAAAcYH3lzk4366.png-wh_50

1
2
[root@Nginx conf] # service mysqld stop
停止 mysqld:                                              [确定]

wKioL1iZTG3hdLiEAAAeSI5okeY100.png-wh_50

更多fastcgi模块属性详见:http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html


本文转自 元婴期 51CTO博客,原文链接:http://blog.51cto.com/jiayimeng/1895585


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值