大神教你Nginx常用基础配置方案
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’]
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;
}
location ~ /ssi_include/ {
if (!-e $request_filename) {
rewrite ^(.*)$ /blank.html last;
}
}
location ~(\.html|\.htm|\.shtml)$ {
error_page 404 500 502 503 504 /404.html;
}
}
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
第一种反向代理:
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;
}
#设置该域名转发给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常用基础配置方案的更多相关文章
- 大神教你零基础学PS,30堂课从入门到精通
ps视频教程,ps自学视频教程.ps免费视频教程下载,大神教你零基础学PS教程视频内容较大,分为俩部分: 大神教你零基础学PS--30堂课从入门到精通第一部分:百度网盘,https://pan.bai ...
- Nginx 常用基础模块
目录 Nginx 常用基础模块 Nginx日志管理 nginx日志切割 Nginx目录索引 Nginx状态监控 Nginx访问控制 Nginx访问限制 Nginx 请求限制配置实战 Nginx Loc ...
- nginx 的基础配置[转]
nginx 的基础配置 分类: 工具软件2013-11-13 23:26 11人阅读 评论(0) 收藏 举报 目录(?)[-] 管理配置文件 全局配置 虚拟机server配置 location配置 ...
- 文章如何做伪原创 SEO大神教你几招做"原创"网站文章的心得
想要创作出好的文章并被百度所喜欢,就非常需要SEO的优化能力,以及要对文章进行塬创或伪塬创,那么,如何做伪塬创文章?以及如何做好塬创网站文章呢?对此,本文小编就为大家带来了几招做"塬创&qu ...
- Nginx常用功能配置一
Nginx常用功能配置 参数include配置 说明:如果日常工作中server标签存在太多,可以采用include配置模式,Nginx的主配置文件包含的所有虚拟主机的子配置文件会统一放入extra目 ...
- 2.了解nginx常用的配置
作者 微信:tangy8080 电子邮箱:914661180@qq.com 更新时间:2019-07-10 20:56:10 星期三 欢迎您订阅和分享我的订阅号,订阅号内会不定期分享一些我自己学习过程 ...
- Nginx常用功能配置二
Nginx常用功能配置二 Nginx location匹配设置 location作用:可以根据用户请求的URI来执行不同的应用,根据用户请求的网站的地址URL匹配. location语法: locat ...
- 2.4 Nginx服务器基础配置指令
2.4.1 nginx.conf文件的结构 2.4.2配置运行Nginx服务器用户(组) 2.4.3配置允许生成的worker process数 2.4.4 配置Nginx进程PID存放路径 2.4. ...
- nginx入门篇----nginx服务器基础配置
1.nginx.conf文件结构... #全局块 events{ ... } http #http块{ ...
随机推荐
- DevExpress换肤
procedure TForm1.cxComboBox1PropertiesChange(Sender: TObject); begin // 这个地方必须是UserSkin,不然不会起作用 dxSk ...
- PHP自动加载SPL的四种处理方式
libs目录下有3个类文件: Test.class.php <?php class Test { public function __construct() { echo "Loadi ...
- [转]记解决一次“HTTP Error 400. The request URL is invalid”的错误
今天将图片服务切到使用了cdn的机器上面去,然后就部分图片报如下图错误“HTTP Error 400. The request URL is invalid” 看到这种错误信息,一般的开发者心中可能会 ...
- CentOS6.5安装Elasticsearch5.3.0
1. 首页到官方网站下载最新安装包 https://www.elastic.co/downloads/elasticsearch elasticsearch-5.3.0.tar.gz 2. 将软件包上 ...
- mac date
格式化UTC为可读格式 mbp:~ gavin$ date -r 1546848158 2019年 1月 7日 星期一 16时02分38秒 CST 获取当前 UTC mbp:~ gavin$ date ...
- iOS UI进阶-6.0 手势
给每个页面添加手势,只需要统一设置不是根控制器的页面,都增加手势.需要自定义导航控制器 1.继承代理 @interface BSNavigationController ()<UIGesture ...
- cocos2dx 3.x(加载网络自定义头像)
// // Connection.h // XXDemo // // Created by LeeHonGee on 14-9-4. // // #ifndef __XXDemo__Connec ...
- RNN通俗理解
让数据间的关联也被 NN 加以分析,我们人类是怎么分析各种事物的关联,?最基本的方式,就是记住之前发生的事情. 那我们让神经网络也具备这种记住之前发生的事的能力. 再分析 Data0 的时候, 我们把 ...
- webpack打包报错
Invalid configuration object. Webpack has been initialised using a configuration object that does no ...
- .NET 黑魔法 - 自定义日志扩展
我们开发程序时避免不了要有日志系统,我们希望有一个通用的.不夹杂任何方言的调用方式,简单地说就是保留微软日志框架的注入方式和使用方式. 比如我们希望这样调用: 我们不希望有个 IAbcLogger, ...