nginx反向代理功能使用(二)

12,自定义响应首部
upstream_cache_status;
server_addr
通知客户端是由哪个主机代理,代理缓存是否命中。
node1
# vim /etc/nginx/conf.d/default.conf
server {
listen 80 default_server;
server_name _;
add_header X-Via $server_addr; 代理服务器的地址
add_header X-Cache $upstream_cache_status;
# service nginx reload
PS:这里需要开启缓存功能
# vim /etc/nginx/conf.d/default.conf location /bbs/ { proxy_cache mark; proxy_cache_valid 200 1d; proxy_cache_valid 301 302 10m; proxy_cache_valid any 1m; proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504; proxy_pass http://markserver/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}

13,fastcgi LNMP
发起对nginx的php请求,nginx会将php代码返回给发起者,为了能够解释php资源,fastcgi能够接收.php文件,而后返回给发起者
这里的php-fpm是安装在nginx代理上的
node1
1,安装php-fpm
[cc lang="php"]
# yum -y install php-fpm
# vim /etc/nginx/nginx.conf 配置文件
# service php-fpm start
[/cc]
2,基于fastcgi发送给php
# vim /etc/nginx/conf.d/default.conf
启用59行的注释
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;
}
把14行的 root /usr/share/nginx/html;复制到这里
location ~ \.php$ { root /usr/share/nginx/html; fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
3,新建测试文件
# cd /usr/share/nginx/html/
# ls
404.html 50x.html index.html nginx-logo.png poweredby.png
# mv index.html nginx.html
# vim index.php

重读nginx配置
#service nginx reload
4在/etc/nginx下fastcgi_params定义了很多内容
删除fastcgi_params内容,添加如下内容
.,$d
# vim /etc/nginx/fastcgi_params
[cc lang="php"]
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;
[/cc]
# /etc/init.d/nginx reload
Reloading nginx: ok
5,在测试就ok了 6,安装php-mysql
#yum -y install php-mysql
使用# rpm -ql php-mysql查看.ini文件,打开浏览器查看php页面的Additional .ini files parsed 这项中是否存在php-mysql的.ini文件,如果有,就可以支持mysql了 7,安装mysql
#yum -y install mysql-server
#service mysqld start
#ss -tnl |grep :3306
LISTEN 0 50 *:3306 *:*
8,修改index.php文件
# vim /usr/share/nginx/html/index.php
[cc lang="php"]
< ?php $conn = mysql_connect('172.0.0.1','root',''); if ($conn) echo succ; else echo fail; mysql_close(); ?>
[/cc]
在打开浏览器,运行显示succ,关闭显示fail
14,fastcgi_cache
proxy_cache
fastcgi_cache
node1
interactive 非活动时间
max_size 最大空间
keys_zone=fcgicache:10m 10m大小
#vim /etc/nginx/nginx.conf
[cc lang="php"]
fastcgi_cache_path /cache/fastcgi/ levels=1:1 keys_zone=fcgicache:10m inactive=3s max_size=1g;
[/cc]
#vim /etc/nginx/conf.d/default.conf
[cc lang="php"]
location ~ \.php$ {
fastcgi_cache fcgicache;
fastcgi_cache_valid 200 10m;200响应码缓存10分钟
fastcgi_cache_valid 302 3m; 302响应码,缓存3分钟
fastcgi_cache_valid any 1m; 其他,只缓存1分钟
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
#
[/cc]

0 分享

您可以选择一种方式赞助本站

支付宝扫码赞助

支付宝扫码赞助

日期: 2015-06-04分类: Nginx

标签: nginx

发表评论