Nginx学习日记第五篇 -- upstream及fastcgi
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.在http区段定义服务器组 [[email protected] 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中引用服务器组 [[email protected] 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 [[email protected] conf]# yum install php-fpm 2.如果php与nginx不在同一台主机,则修改/etc/php-fpm.conf中的监听地址,我这里在一起,不用修改 3.启动php [[email protected] conf]# service php-fpm start Nginx主机配置 1.Nginx配置,启用了默认的PHP配置 [[email protected] 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 [[email protected] 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 [[email protected] conf]# nginx -s reload 4.给出php页面 [[email protected] html]# vim index.php <?php phpinfo(); ?>
3、测试
4、安装php-mysql驱动mysql(都在本机,不在同一机器更改监听端口即可)
1.安装php-mysql [[email protected] conf]# yum install php-mysql 2.安装mysql [[email protected] conf]# yum install mysql-server 3.重启php,启动mysql [[email protected] conf]# service php-fpm restart 停止 php-fpm: [确定] 正在启动 php-fpm: [确定] [[email protected] 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连接
[[email protected] 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(); ?>
[[email protected] 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
以上是关于Nginx学习日记第五篇 -- upstream及fastcgi的主要内容,如果未能解决你的问题,请参考以下文章
Python 学习日记第五篇 -- collections系列
MySQL之存储引擎大全 -《从0到1-全面深刻理解MySQL系列-第五篇》
nginx之旅(第五篇):URL重写介绍URL重写场景URL重写语法