先上配置的过程,下面是解释。
1.首先,我们有必要先了解一下Nginx配置文件里指令的继承关系:
Nginx配置文件分为好多块,常见的从外到内依次是「http」、「server」、「location」等等,缺省的继承关系是从外到内,也就是说内层块会自动获取外层块的值作为缺省值。
让我们先从「index」指令入手吧
在问题配置中它是在「location」中定义的:
一旦未来需要加入新的「location」,必然会出现重复定义的「index」指令,这是因为多个「location」是平级的关系,不存在继承,此时应该在「server」里定义「index」,借助继承关系,「index」指令在所有的「location」中都能生效。
2.接下来看看「if」指令
说它是大家误解最深的Nginx指令毫不为过:
很多人喜欢用「if」指令做一系列的检查,不过这实际上是「try_files」指令的职责:
try_files $uri $uri/ /index.php
除此以外,初学者往往会认为「if」指令是内核级的指令,但是实际上它是rewrite模块的一部分,加上Nginx配置实际上是声明式的,而非过程式的,所以当其和非rewrite模块的指令混用时,结果可能会非你所愿。
3.下面看看「fastcgi_params」配置文件
include fastcgi_params
Nginx有两份fastcgi配置文件,分别是「fastcgi_params」和「fastcgi.conf」,它们没有太大的差异,唯一的区别是后者比前者多了一行「SCRIPT_FILENAME」的定义:
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name
注意:$document_root 和 $fastcgi_script_name 之间没有
/。
原本Nginx只有「fastcgi_params」,后来发现很多人在定义「SCRIPT_FILENAME」时使用了硬编码的方式,于是为了规范用法便引入了「fastcgi.conf」。
不过这样的话就产生一个疑问:为什么一定要引入一个新的配置文件,而不是修改旧的配置文件?这是因为「fastcgi_param」指令是数组型的,和普通指令相同的是:内层替换外层;和普通指令不同的是:当在同级多次使用的时候,是新增而不是替换。换句话说,如果在同级定义两次「SCRIPT_FILENAME」,那么它们都会被发送到后端,这可能会导致一些潜在的问题,为了避免此类情况,便引入了一个新的配置文件。
此外,我们还需要考虑一个安全问题:在PHP开启「cgi.fix_pathinfo」的情况下,PHP可能会把错误的文件类型当作PHP文件来解析。如果Nginx和PHP安装在同一台服务器上的话,那么最简单的解决方法是用「try_files」指令做一次过滤:
try_files $uri =404
4.下面就是如何配置的过程:
先安装编译依赖的一些组件复制代码 代码如下:
yum install pcre pcre-devel openssl openssl-devel -y
1、解压程序包
复制代码 代码如下:
tar xf nginx-1.10.0.tar.gz
cd nginx-1.10.0
2、预编译配置参数
复制代码 代码如下:
./configure --user=www \
--group=www \
--prefix=/data/server/nginx \
--with-http_stub_status_module \
--without-http-cache \
--with-http_ssl_module \
--with-http_gzip_static_module
3、执行编译
复制代码 代码如下:
make &&make install
4、替换配置文件
nginx.conf
user www www
worker_processes 1
error_log /u01/data/log/nginx/error.log crit
pid /u01/data/server/nginx/logs/nginx.pid
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535
events
{
use epoll
worker_connections 65535
}
http {
include mime.types
default_type application/octet-stream
#charset gb2312
server_names_hash_bucket_size 128
client_header_buffer_size 32k
large_client_header_buffers 4 32k
client_max_body_size 8m
sendfile on
tcp_nopush on
keepalive_timeout 60
tcp_nodelay on
fastcgi_connect_timeout 300
fastcgi_send_timeout 300
fastcgi_read_timeout 300
fastcgi_buffer_size 64k
fastcgi_buffers 4 64k
fastcgi_busy_buffers_size 128k
fastcgi_temp_file_write_size 128k
gzip on
gzip_min_length 1k
gzip_buffers 4 16k
gzip_http_version 1.0
gzip_comp_level 2
gzip_types text/plain application/x-javascript text/css application/xml
gzip_vary on
#limit_zone crawler $binary_remote_addr 10m
log_format main '$remote_addr - "$request_time" [$time_local] "$request" '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for'
log_format '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for "$request_time"'
include /u01/alidata/server/nginx/conf/vhosts/*.conf
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)