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

2023年 7月 15日 80.4k 0

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;
}

2015-06-03_235849fastcgi

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了 2015-06-03_180229php-fpm 6,安装php-mysql
#yum -y install php-mysql
使用# rpm -ql php-mysql查看.ini文件,打开浏览器查看php页面的Additional .ini files parsed 这项中是否存在php-mysql的.ini文件,如果有,就可以支持mysql了 2015-06-03_180715php-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] 2015-06-03_190029facgicache

相关文章

LeaferJS 1.0 重磅发布:强悍的前端 Canvas 渲染引擎
10分钟搞定支持通配符的永久有效免费HTTPS证书
300 多个 Microsoft Excel 快捷方式
一步步配置基于kubeadmin的kubevip高可用
istio全链路传递cookie和header灰度
REST Web 服务版本控制

发布评论