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


相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
缓存 负载均衡 应用服务中间件
Nginx 学习
【10月更文挑战第17天】Nginx 是一款非常强大的工具,掌握它的使用和配置对于构建高性能、可靠的 Web 应用至关重要。随着技术的不断发展,Nginx 也在不断更新和完善,为我们提供更好的服务和支持。
432 59
|
负载均衡 应用服务中间件 Linux
nginx学习,看这一篇就够了:下载、安装。使用:正向代理、反向代理、负载均衡。常用命令和配置文件,很全
这篇博客文章详细介绍了Nginx的下载、安装、配置以及使用,包括正向代理、反向代理、负载均衡、动静分离等高级功能,并通过具体实例讲解了如何进行配置。
867 5
nginx学习,看这一篇就够了:下载、安装。使用:正向代理、反向代理、负载均衡。常用命令和配置文件,很全
|
Kubernetes 应用服务中间件 nginx
k8s学习--YAML资源清单文件托管服务nginx
k8s学习--YAML资源清单文件托管服务nginx
608 2
k8s学习--YAML资源清单文件托管服务nginx
|
Kubernetes 监控 测试技术
k8s学习--基于Ingress-nginx实现灰度发布系统
k8s学习--基于Ingress-nginx实现灰度发布系统
978 2
k8s学习--基于Ingress-nginx实现灰度发布系统
|
缓存 负载均衡 算法
nginx学习:配置文件详解,负载均衡三种算法学习,上接nginx实操篇
Nginx 是一款高性能的 HTTP 和反向代理服务器,也是一个通用的 TCP/UDP 代理服务器,以及一个邮件代理服务器和通用的 HTTP 缓存服务器。
866 0
nginx学习:配置文件详解,负载均衡三种算法学习,上接nginx实操篇
|
Kubernetes 负载均衡 应用服务中间件
k8s学习--ingress详细解释与应用(nginx ingress controller))
k8s学习--ingress详细解释与应用(nginx ingress controller))
3003 0
|
Ubuntu 应用服务中间件 Linux
Linux学习之Ubuntu 20中OpenResty的nginx目录里内容和配置文件
总的来说,OpenResty的Nginx配置文件是一个强大的工具,它允许你以非常灵活的方式定义你的Web服务的行为。
774 2
|
负载均衡 Java 应用服务中间件
学习aop以及nginx
学习AOP和Nginx的最好方式是通过实践。你可以尝试在你的项目中使用这些技术,或者找一些在线教程进行学习。
184 1
|
JavaScript 前端开发 应用服务中间件
Vue学习:webpack-dev-server和nginx问答
Vue学习:webpack-dev-server和nginx问答
|
负载均衡 前端开发 中间件
nginx安装配置 - 全网最细学习nginx
nginx安装配置 - 全网最细学习nginx
301 0