当前位置首页 > Nginx知识

nginx初级安装配置

阅读次数:247 次  来源:admin  发布时间:

实验环境:系统 CENTOS5.5,用户 root

目录以yum默认安装为参考

一、安装

1、安装epel源(安装时注意版本选择,此处为5.X 32位版)

rpm -ivh http://mirrors.ustc.edu.cn/fedora/epel//5/i386/epel-release-5-4.noarch.rpm

安装nginx以及php模块

yum install nginx php-fpm php-cgi

2、如果以前用的是apache,记得要停掉

/etc/init.d/httpd sto

chkconfig httpd --level 2345 off

3、启动nginx

chkconfig httpd --level 2345 o

/etc/init.d/nginx start

二、常用操作

1、启动

/usr/sbin/nginx -c /etc/nginx/nginx.conf

不使用-c指定配置文件的话,默认加载安装目录下conf/nginx.conf

2、停止

平衡停止

kill -QUIT <nginx pid>

kill -QUIT `cat /var/run/nginx.pid`

3、重启

/usr/sbin/nginx -s reload

kill -HUP `cat /var/run/nginx.pid`

4、配置文件检查

/usr/sbin/nginx -t -c /etc/nginx/nginx.conf

5、查看版本

简单显示版本号

/usr/sbin/nginx -v

详细显示版本及config相关信息

/usr/sbin/nginx -V

三、基本配置

1、nginx.conf基础配置

#工作用户及用户组(根据机器环境修改配置)

user nginx nginx

#工作进程数(一般设为CPU总核数或其两倍)

worker_processes 8

#错误日志路径及记录级别(debug,info,notice,warn,error,crit)

error_log /var/log/nginx/error.log war

#pid保存路径

id /var/run/nginx.pid

#文件描述符数

worker_rlimit_nofile 51200

event

{

#使用的网络I/O模型,linux推荐epoll模型,freebsd推荐kqueue模型

use epoll

#允许的连接数,可以的话尽量设大一些

worker_connections 51200

}

htt

{

include /etc/niginx/mime.type

defaut_type application/octet-stream

#默认字符集,如不确定网站字符集,则不要设置,通过html的meta标签指定。

charset utf-8

#禁止错误页面里显示nginx的版本号

erver_tokens off

erver_names_hash_bucket_size 128

client_header_buffer_size 32k

large_client_header_buffers 4 32k

#客户端上传文件大小限制

client_max_body_size 8m

endfile o

tcp_nopush o

#客户端连接超时,服务器将关闭连接。

keepalive_timeout 60

tcp_nodelay o

#开启gzip压缩

gzip o

#小于设置大小的文件不压缩

gzip_min_length 1k

gzip_buffers 4 16k

gzip_http_version 1.1

#压缩等级

gzip_comp_level 2

#压缩文件的类型

gzip_types text/plain application/x-javascript text/css application/xml

gzip_vary o

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

#指定是否传递错误信息到客户端,或者允许nginx使用error_page处理错误信息。

fastcgi_intercept_errors off

erver

{

#详见 站点配置

}

}

2、去除fastcgi中nginx版本号相关设置

编辑/usr/local/nginx/conf/fastcgi.conf和/usr/local/nginx/conf/fastcgi_param

将fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;改成fastcgi_param SERVER_SOFTWARE nginx

3、防止被绑定域名

erver {

listen 80 default

rewrite ^.* http://www.test.com permanent; #此处域名指向自己主站,以引导网友访问正确网站。

}

4、禁止某目录下的某类文件访问

location ~* ^/(<目录名可以是正则>)/.*\.(<文件扩展名可以是正则>)$

{

deny all

}

例如:

location ~* ^/(images|files)/.*\.(php|php3|php4|php5|cgi)$

{

deny all

}

5、防止php_info BUG

编辑/usr/local/nginx/conf/fastcgi_params,首行添加如下内容

if ($request_filename ~* (.*)\.php)

{

et $php_url $1

}

if (!-e $php_url.php)

{

return 403

}

6、防旁注

各个虚拟主机目录设为不易猜测的名字。

在php.ini中修改open_basedir值,设置为上传文件的临时目录和各虚拟主机存放目录(以冒号分隔)。

chmod 755 -R <虚拟主机目录>

chmod 711 <虚拟主机目录>

7、防盗链

以防图片盗链为例,只要请求来源非www.test.com则一概返回403错误.

location ~* .(gif|jpg|png)$ {

valid_referers none blocked www.test.com

if ($invalid_referer) {

return 403

}

}

四、站点配置

每组server{}是一个虚拟主机。

1、基于IP的虚拟主机

erver

{

#监听IP及端口

listen 192.168.1.2:80

#主机名

erver_name 192.168.1.2

#日志文件存放路径

access_log logs/host.access.log mai

location /

{

#默认首页文件,从左至右匹配

index index.html index.htm

#网站目录

root /opt/web/server1

}

}

2、基于域名的虚拟主机

erver

{

#监听端口

listen 80

#主机名,表示处理所有test1.test.com、test2.test.com以及*.test.cn域名的访问。

erver_name test1.test.com test2.test.com *.test.c

#日志文件存放路径

access_log logs/host.access.log mai

location /

{

index index.html index.htm

root /opt/web/server1

}

}

五、日志处理

1、log_format

基本格式:log_format <日志格式名> <格式定义串>

示例:log_format gzip '$remote_addr - $remote_user [$time_local] '

'"$request" $status $bytes_sent '

'"$http_referer" "$http_user_agent" "$gzip_ratio"'

2、access_log

基本格式: access_log <日志路径> [日志格式名(通过log_format定义)] [buffer=SIZE]

示例:access_log /var/log/web/test.log gzip buffer=32k

3、日志文件切割

·创建脚本,输入以下内容,保存到恰当的位置,例如/opt/cut_log.sh

#! /bin/bash

#功能说明:自动分割压缩日志文件,保存最近15天日志

EXPIRES_DAY=15

BAK_NAME=$(date -d "yesterday" +"%Y%m%d")

LOG_PARENT_PATH="/var/log/nginx/"

LOG_PATH="access/"

mkdir -p ${LOG_PARENT_PATH}${LOG_PATH}

mv ${LOG_PARENT_PATH}access.log ${LOG_PARENT_PATH}${LOG_PATH}${BAK_NAME}.log

kill -USR1 `cat /var/run/nginx.pid`

cd ${LOG_PARENT_PATH}${LOG_PATH} && tar -czf ${BAK_NAME}.tgz ${BAK_NAME}.log

rm -f ${LOG_PARENT_PATH}${LOG_PATH}${BAK_NAME}.log

find ${LOG_PARENT_PATH}${LOG_PATH} -name '*.tgz' -ctime ${EXPIRES_DAY} -exec rm {} \

·添加到计划任务,每天0时0分自动执行

crontab -u root -e

0 0 * * * /opt/cut_log.sh

六、rewrite处理

1、 if

基本格式:if (<判断条件>){...}

不能嵌套,不能多条件判断

支持的判断条件:

~ 区分大小写

~* 不区分大小写

-f 判断文件是否存在

-d 判断目录是否存在

-e 判断文件或目录是否存在

-x 判断文件是否可执行

判断符前加!表示不匹配,如!-f 表示匹配文件不存在

和括号中正则匹配的内容,之后可以用变量$1至$9调用,比如:

if ($request_uri ~* "/test/(\d+)\.html")

{

et $numb $1

rewrite ^(.*)$ /msie/$1/$numb.html break

}

注:上例中有两个匹配项,set $numb $1;中的$1匹配的是"/test/(\d+)\.html"中的(\d+)。

rewrite ^(.*)$ /msie/$1/$numb.html break;中的$1匹配的是(.*)。

2、retur

基本格式:return <状态码>

返回状态码,包括204、400、402~406、408、410、411、413、416和500~504。

3、rewrite

基本格式:rewrite <正则式> <修改后的链接> <标志>

重定向符合标准的链接至修改后的链接。

重写表达式只对相对路径有效。例如:

#访问URL: http://www.test.com/main/index.htm

if ($host ~* www\.(.*))

{

et $host_without_www $1

rewrite ^(.*)$ http://$host_without_www$1 permanent; #此处$1的内容为"/main/index.htm"

}

标志定义:

last:完成rewrite,使用alias指令时必须用此标记。

reak:此规则匹配完成后,终止匹配。使用proxy_pass指令时使用此标记。

redirect:302重定向。

ermanent:301重定向。

last在本条rewrite规则执行完后,会对所在的server{}重新发请求,而break则在匹配完后终止匹配。

因此,一般在location /{}或直接在server{}中的rewrite规则里使用last,在非根location中使用break。

如URI中有参数(例:http://www.test.com/index.htm?id=10),默认情况参数会自动附到替换串上,如果不需要附带参数,则在替换串末尾加上“?”。

4、set

基本格式:set <变量名> <值>

用于定义变量或变量赋值。例如:

et $name 'lykyl'

附: nginx支持的信号

TERM INT 快速关闭

QUIT 从容关闭

HUP 平滑重启

USR1 重新打开或建立日志文件

USR2 平滑升级可执行程序

WINCH 从容关闭工作进程

附:nginx日志变量

$body_bytes_sent the number of bytes sent to a client not counting the response header; this variable is compatible with the “%B” parameter of the mod_log_config Apache module

$bytes_sent the number of bytes sent to a client

$connection connection serial number

$connection_requests the current number of requests made through a connectio

$msec time in seconds with a milliseconds resolution at the time of log write

$pipe “p” if request was pipelined, “.” otherwise

$request_length request length (including request line, header, and request body)

$request_time request processing time in seconds with a milliseconds resolution; time elapsed between the first bytes were read from the client and the log write after the last bytes were sent to the client

$status response statu

$time_iso8601 local time in the ISO 8601 standard format

$time_local local time in the Common Log Format

附:nginx rewrite全局变量

$arg_PARAMETER 包含GET请求中,如果有变量PARAMETER时的值。

$args 请求行中(GET请求)的参数。

$binary_remote_addr #二进制的客户地址。

$body_bytes_sent #响应时送出的body字节数数量。即使连接中断,这个数据也是精确的。

$content_length #请求头中的Content-length字段。

$content_type #请求头中的Content-Type字段。

$cookie_COOKIE #cookie COOKIE变量的值

$document_root #当前请求在root指令中指定的值。

$document_uri #与$uri相同。

$host #请求主机头字段,否则为服务器名称。

$hostname #Set to the machine’s hostname as returned by gethostname

$http_HEADER

$is_args #如果有$args参数,这个变量等于”?”,否则等于”",空值。

$http_user_agent #客户端agent信息

$http_cookie #客户端cookie信息

$limit_rate #这个变量可以限制连接速率。

$query_string #与$args相同。

$request_body_file #客户端请求主体信息的临时文件名。

$request_method #客户端请求的动作,通常为GET或POST。

$remote_addr #客户端的IP地址。

$remote_port #客户端的端口。

$remote_user #已经经过Auth Basic Module验证的用户名。

$request_completion #如果请求结束,设置为OK. 当请求未结束或如果该请求不是请求链串的最后一个时,为空(Empty)。

$request_method #GET或POST

$request_filename #当前请求的文件路径,由root或alias指令与URI请求生成。

$request_uri #包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。不能修改。

$scheme #HTTP方法(如http,https)。

$server_protocol #请求使用的协议,通常是HTTP/1.0或HTTP/1.1。

$server_addr #服务器地址,在完成一次系统调用后可以确定这个值。

$server_name #服务器名称。

$server_port #请求到达服务器的端口号。

$uri #不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。该值有可能和$request_uri 不一致。$request_uri是浏览器发过来的值。该值是rewrite后的值。例如做了internal redirects后。

上一篇:Redis介绍以及安装(Linux)Redis介绍以及安装(Linux)
下一篇:使用IIS7.0/7.5时配置HttpModules需要注意