Nginx的fastcgi模块参数设置

Nginx 有两个配置文件fastcgi_params、fastcgi.conf,两者唯一的区别是,fastcgi.conf 多一个参数 SCRIPT_FILENAME,diff显示如下:

$diff fastcgi fastcgi_params

< fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
vim 进入/usr/local/nginx/conf/fastcgi_params文件 #请求的参数;如?app=123fastcgi_param
fastcgi_param QUERY_STRING $query_string; ##请求的动作(GET,POST)
fastcgi_param REQUEST_METHOD $request_method; #请求头中的Content-Type字段
fastcgi_param CONTENT_TYPE $content_type; #请求头中的Content-length字段
fastcgi_param CONTENT_LENGTH $content_length; #脚本名称
fastcgi_param SCRIPT_NAME $fastcgi_script_name; #请求的地址不带参数
fastcgi_param REQUEST_URI $request_uri; #与$uri相同
fastcgi_param DOCUMENT_URI $document_uri; #网站的根目录。在server配置中root指令中指定的值
fastcgi_param DOCUMENT_ROOT $document_root; #请求使用的协议,通常是HTTP/1.0或HTTP/1.1
fastcgi_param SERVER_PROTOCOL $server_protocol; #https 如果value非空才进行设置
fastcgi_param HTTPS $https if_not_empty; #cgi 版本
fastcgi_param GATEWAY_INTERFACE CGI/1.1; #nginx 版本号,可修改、隐藏
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; #客户端IP
fastcgi_param REMOTE_ADDR $remote_addr; #客户端端口
fastcgi_param REMOTE_PORT $remote_port; #服务器IP地址
fastcgi_param SERVER_ADDR $server_addr; #服务器端口
fastcgi_param SERVER_PORT $server_port; #服务器名,域名在server配置中指定的server_name
fastcgi_param SERVER_NAME $server_name; 可自定义变量
fastcgi_param PATH_INFO $path_info; #在尾部另起一行追加即可保存跟fastcgi.conf 一致
fastcgi_param REDIRECT_STATUS 200;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 在php可打印出上面的服务环境变量
如:echo $_SERVER[‘REMOTE_ADDR’]
Nginx的常用指令解释
fastcgi_pass

这个命令是指定将http代理到哪个fastcgi服务端接口。fastcgi_pass后面是填写fastcgi服务端地址的,这个地址可以是域地址,也可以是Uninx-域套接字, 另外也可以是upstream中设置的反向代理。

fastcgi_pass localhost:9000; #默认PHP起在9000端口
fastcgi_pass unix:/tmp/fastcgi.socket;
fastcgi_pass upstream_php5; #这里指定的反向代理可以在nginx.conf中upstream中设置
fastcgi_param 这个命令是设置fastcgi请求中的参数,默认参数在上面提到的fastcgi模块参数文件中,具体设置的东西可以在$_SERVER中获取到。
比如你想要设置当前的机器环境,可以使用fastcgi_param ENV test;来设置。
对于php来说,最少需要设置的变量有: fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_index 这个命令设置了fastcgi默认使用的脚本。就是当SCRIPT_FILENAME没有命中脚本的时候,使用的就是fastcgi_index设置的脚本。 fastcgi_index index.php;

以上三个命令能组成最基本的fastcgi设置了:

location / {
fastcgi_pass localhost:9000;
fastcgi_index index.php; #下面这一个可以直接在fastcgi_param配置文件中指定
fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
}
图片(或者静态文件)服务器配置
server {
listen 80;
server_name images.xxx.com img.movie.xxx.com;
root /data/vhosts/xxx.com/images/public_html/;
index index.shtml index.html index.htm; #如果是js、css、json文件可以指定压缩来减少传输文件大小
gzip_types text/plain application/x-javascript text/css application/xml text/xml application/json;
}
基础服务器
server {
listen 80;
server_name www.xxx.com;
root /data/vhosts/xxxx.com/public_html/;
index index.htm index.php index.html index.shtml; location / {
ssi on;
ssi_silent_errors on;
ssi_types text/shtml;
include other.conf; #这里可以配置其他的公共配置,或者重写规则
} location ~\.php$ {
expires off;
include fastcgi_params; #fastcgi 指定的参数配置
fastcgi_pass 127.0.0.1:9000; #这里同上也可指定代理或socket
fastcgi_index index.php;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
配置ssi_inclue访问的目录不存在是指定的目录
location ~ /ssi_include/ {
if (!-e $request_filename) {
rewrite ^(.*)$ /blank.html last;
}
}
配置静态文件默认的错误页面
location ~(\.html|\.htm|\.shtml)$ {
error_page 404 500 502 503 504 /404.html;
}
}
Auth权限设置

step 1. 在根域名下面需要配置权限的目录设置location

location /phpMyAdmin/ {
allow 192.168.0.1;
allow xx.xx.xxx.xxx;
allow xx.xx.xxx.xxx;
deny all;
auth_basic "Restricted";
auth_basic_user_file /usr/local/nginx/conf/auth_phpmyadmin.pass;
}

step2. 在 auth_basic_user_file 指定的文件下面增加账号密码,一行一个

username1:password1
username2:password2
username3:password3
username4:password4
Nginx反向代理

第一种反向代理:

location / {
proxy_pass http://192.168.1.4:8099/;
#若针对不同的目录进行代理把下面的配置放到根目录代理的上面
#proxy_pass http://192.168.1.4:8099/linuxtone/;
proxy_redirect default ;
}

第二种反向代理:

upstream配置
upstream xx.xxx.com {
server 192.168.1.4:8099;
}
站点配置文件
server
{
listen 80;
server_name bbs.linuxtone.conf;
index index.html index.htm;
root /date/vhosts/xxx.com/; location ~ ^/NginxStatus/ {
stub_status on;
access_log off;
}
location / {
proxy_redirect off ;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 50m; #缓冲区代理缓冲用户端请求的最大字节数,可以理解为保存到本地再传给用户
client_body_buffer_size 256k;
proxy_connect_timeout 30; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout 30;
proxy_read_timeout 60; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size 256k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 256k; #proxy_buffers缓冲区,网页平均在256k以下的话,这样设置
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
proxy_ignore_client_abort on; #不允许代理端主动关闭连接
#http://xx.xxx.com 指上面upstream块的名称
proxy_pass http://xx.xxx.com;
}
Apache反向代理

#设置该域名转发给8080端口

ServerAdmin webmaster@dummy-host2.example.com
ServerName www.xxx.com
ProxyRequests off
Order deny,allow
Allow from all
ProxyPass / http://www.xxx.com:8080/
ProxyPassReverse / http://www.xxx.com:8080/

ProxyPassReverse一般和ProxyPass指令配合使用,此指令使Apache调整HTTP重定向应答中Location, Content-Location, URI头里的URL,这样可以避免在Apache作为反向代理使用时,。后端服务器的HTTP重定向造成的绕过反向代理的问题

禁止蜘蛛访问

#判断UA,如果UA不包含spider或者bot(不区分大小写),表示UA为正常用户

#设置变量is_human值为yes

if ($http_user_agent !~* "spider|bot") {
set $is_human 'yes';
}

#当有任意请求的时候,该UA不是正常用户,则表示应该是蜘蛛类程序,则返回403

location / {
if ($is_human != 'yes') {
return 403;
}
}

#当有任意请求的时候

location / {
#当访问者UA包含有spider或则bot的时候(不区分大小写),说明是蜘蛛类来访
if ($http_user_agent ~* "spider|bot") {
# 直接就屏蔽蜘蛛的整站访问
return 403;
}
}

给系统添加了robots.txt文件:

User-agent: *
Disallow: /

大神教你Nginx常用基础配置方案的更多相关文章

  1. 大神教你零基础学PS,30堂课从入门到精通

    ps视频教程,ps自学视频教程.ps免费视频教程下载,大神教你零基础学PS教程视频内容较大,分为俩部分: 大神教你零基础学PS--30堂课从入门到精通第一部分:百度网盘,https://pan.bai ...

  2. Nginx 常用基础模块

    目录 Nginx 常用基础模块 Nginx日志管理 nginx日志切割 Nginx目录索引 Nginx状态监控 Nginx访问控制 Nginx访问限制 Nginx 请求限制配置实战 Nginx Loc ...

  3. nginx 的基础配置[转]

    nginx 的基础配置 分类: 工具软件2013-11-13 23:26 11人阅读 评论(0) 收藏 举报   目录(?)[-] 管理配置文件 全局配置 虚拟机server配置 location配置 ...

  4. 文章如何做伪原创 SEO大神教你几招做"原创"网站文章的心得

    想要创作出好的文章并被百度所喜欢,就非常需要SEO的优化能力,以及要对文章进行塬创或伪塬创,那么,如何做伪塬创文章?以及如何做好塬创网站文章呢?对此,本文小编就为大家带来了几招做"塬创&qu ...

  5. Nginx常用功能配置一

    Nginx常用功能配置 参数include配置 说明:如果日常工作中server标签存在太多,可以采用include配置模式,Nginx的主配置文件包含的所有虚拟主机的子配置文件会统一放入extra目 ...

  6. 2.了解nginx常用的配置

    作者 微信:tangy8080 电子邮箱:914661180@qq.com 更新时间:2019-07-10 20:56:10 星期三 欢迎您订阅和分享我的订阅号,订阅号内会不定期分享一些我自己学习过程 ...

  7. Nginx常用功能配置二

    Nginx常用功能配置二 Nginx location匹配设置 location作用:可以根据用户请求的URI来执行不同的应用,根据用户请求的网站的地址URL匹配. location语法: locat ...

  8. 2.4 Nginx服务器基础配置指令

    2.4.1 nginx.conf文件的结构 2.4.2配置运行Nginx服务器用户(组) 2.4.3配置允许生成的worker process数 2.4.4 配置Nginx进程PID存放路径 2.4. ...

  9. nginx入门篇----nginx服务器基础配置

    1.nginx.conf文件结构...                         #全局块  events{  ...  }  http                      #http块{ ...

随机推荐

  1. VueI18n的应用

    .npm install vue-i18n .在 main.js 中引入 vue-i18n import VueI18n from 'vue-i18n' Vue.use(VueI18n) .在main ...

  2. java native方法及JNI实例 (转)

    转自:http://blog.csdn.net/xw13106209/article/details/6989415 1.参考文献: http://blog.csdn.net/youjianbo_ha ...

  3. [LeetCode] 129. Sum Root to Leaf Numbers_Medium tag: DFS

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  4. go build -ldflags

    http://studygolang.com/articles/2052 ldflags 用法:[路径,非必需,除非你有目录层次]包名.变量 [path]packege.value go build ...

  5. react 全局公共组件-----动态弹窗 (dialog)

    react 的时候,总是会用到弹窗,并且各种各样的,一般来说,组件层级嵌套之后,就会出现 z-index层级覆盖的问题 这个时候,就需要一个公共的弹出层,然后我们将需要展示的组件,放到弹出层里面 下面 ...

  6. node 图片验证码生成

    var captchapng = require('captchapng'); var http = require("http") var server = http.creat ...

  7. Linux-nmon系统性能监控工具的使用及报表产出

    在进行性能测试的时候,需要获取服务器的各项指标,例如 CPU.MEM.I/O.DISK 等.网上有很多的监控工具,nmon 就是其中的一个,其可与 JMeter结合使用,测试系统的性能.其概要的介绍, ...

  8. 虚拟机 liunx系统以 root 身份登录权限

    开启虚拟机 打开终端开启root账户 :sudo passwd -u root 输入当前用户的密码   为root账户设置密码:sudo passwd root 设置root密码,输入两次   测试r ...

  9. java基础(一) -语法

    基本语法 编写Java程序时,应注意以下几点: 大小写敏感:Java是大小写敏感的,这就意味着标识符Hello与hello是不同的. 类名:对于所有的类来说,类名的首字母应该大写.如果类名由若干单词组 ...

  10. LA 3890 Most Distant Point from the Sea(半平面交)

    Most Distant Point from the Sea [题目链接]Most Distant Point from the Sea [题目类型]半平面交 &题解: 蓝书279 二分答案 ...