当前位置首页 > Nginx知识

nginx

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

1.安装

    a.安装pcre:yum install pcre -y
               yum install pcre-devel
    b.安装zlib-devel:yum install zlib-devel

c.yum install gd-devel openssl-devel -y

c.安装编译:
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --user=zxadmin --group=zxadmin --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi/ --http-scgi-temp-path=/var/tmp/nginx/scgi/ --with-pcre  --with-file-aio --with-http_image_filter_module

make

make install

2.目录结构

[root@server05 ~]# ll /usr/local/nginx/
total 16
drwxr-xr-x. 2 root root 4096 Apr 19 18:01 conf
drwxr-xr-x. 2 root root 4096 Apr 19 18:01 html
drwxr-xr-x. 2 root root 4096 Apr 19 18:01 logs
drwxr-xr-x. 2 root root 4096 Apr 19 18:01 sbin

conf --->配置文件

html --->网页文件

logs --->日志文件

in --->主要的二进制程序

3.启动nginx

/usr/local/nginx/sbin/nginx   #运行此二进制文件

4.nginx进程:

[root@server05 nginx]# ps -aux|grep nginx
root      33732  0.0  0.0  20328   616 ?        Ss   18:06   0:00 nginx: master process ./sbin/nginx    --nginx主进程
nobody    33733  0.0  0.1  20756  1204 ?        S    18:06   0:00 nginx: worker process                 --nginx子进程

5.管理nginx:

/usr/local/nginx/sbin/nginx  -s stop    --fast shutdown(快速停止nginx,很暴力,慎用!)

/usr/local/nginx/sbin/nginx  -s quit    --graceful shutdown(完整有序的停止nginx,用户请求结束后关闭进程)

/usr/local/nginx/sbin/nginx  -s reload  --reloading the configuration file(重载配置)

/usr/local/nginx/sbin/nginx  -s reopen  --reopening the log files(重新打开日志文件)

kill -HUP 33837               --平滑重启,需要指定主进程id。

kill -USR1 33837                       --重读日志,用于日志切割备份,需要指定主进程id。

用其他方法代替PID(每次都要ps获取pid,很麻烦):
[root@server05 logs]# cat /usr/local/nginx/logs/nginx.pid 
33837
-----------------------------------------------------------------
cat /usr/local/nginx/logs/nginx.pid |xargs kill -HUP

cat /usr/local/nginx/logs/nginx.pid |xargs kill -USR1

-----------------------------------------------------------------
nginx -t                                 --测试配置文件是否正确配置

6.配置文件

mkdir /usr/local/nginx/conf/vhost
worker_processes 1;                      --有1个工作的子进程,可以自行修改,但太大无益,因为要争夺CPU,一般设置为 CPU数*核数

Event {

worker_connections 1024; // 一般是配置nginx连接的特性,这是指 一个子进程最大允许连1024个连接

}

http {

include mime.type

default_type application/octet-stream

log_format main '$remote_addr - $remote_user [$time_local] "$request" ' --配置日志格式

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"'

#access_log logs/access.log mai

endfile o

#tcp_nopush o

#keepalive_timeout 0

keepalive_timeout 65

#gzip o

erver { --基于域名的虚拟主机

listen 80

erver_name www.vijay.com

location / {

root /usr/local/nginx/webroot/; --root指虚拟主机根目录,可以写相对路径,也可以写绝对路径 ,此处是绝对路径。

index 1.html

}

access_log logs/www.vijay.com.access.log main; --为此虚拟主机设置专用的日志

}

erver { --基于端口的虚拟主机

listen 8080

erver_name www.vijay.com

location / {

root webroot

index 2.html

}

access_log logs/8080.access.log main;          --为此虚拟主机设置专用的日志

}

erver { --基于IP的虚拟主机

listen 80

erver_name 192.168.100.175

location / {

root webroot; --root指虚拟主机根目录,可以写相对路径,也可以写绝对路径 ,此处是相对路径。

index 3.html

}

access_log logs/192.168.100.175.access.log main; --为此虚拟主机设置专用的日志

}

erver {

listen 80

erver_name localhost

#charset koi8-r

#access_log logs/host.access.log mai

location / {

root html

index ab.html index.html index.htm

}

#error_page 404 /404.html

# redirect server error pages to the static page /50x.html

#

error_page 500 502 503 504 /50x.html

location = /50x.html {

root html

}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80

#

#location ~ \.php$ {

# proxy_pass http://127.0.0.1

#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

#

#location ~ \.php$ {

# root html

# fastcgi_pass 127.0.0.1:9000

# fastcgi_index index.ph

# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name

# include fastcgi_param

#}

# deny access to .htaccess files, if Apache's document root

# concurs with nginx's one

#

#location ~ /\.ht {

# deny all

#}

include /usr/local/nginx/conf/vhost/*;

}

7.日志管理

我们观察nginx的server段,可以看到如下类似信息

#access_log logs/host.access.log mai

这说明 该server, 它的访问日志的文件是 logs/host.access.log ,

使用的格式”main”格式.

除了main格式,你可以自定义其他格式.

main格式是什么?

log_format main '$remote_addr - $remote_user [$time_local] "$request" '

# '$status $body_bytes_sent "$http_referer" '

# '"$http_user_agent" "$http_x_forwarded_for"'

main格式是我们定义好一种日志的格式,并起个名字,便于引用.

以上面的例子, main类型的日志,记录的 remote_addr.... http_x_forwarded_for等选项.

1: 日志格式 是指记录哪些选项

默认的日志格式: mai

log_format main '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"'

如默认的main日志格式,记录这么几项

远程IP- 远程用户/用户时间 请求方法(如GET/POST) 请求体body长度 referer来源信息

http-user-agent用户代理/蜘蛛 ,被转发的请求的原始IP

http_x_forwarded_for:在经过代理时,代理把你的本来IP加在此头信息中,传输你的原始IP

2: 声明一个独特的log_format并命名

log_format mylog '$remote_addr- "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"'

在下面的server/location,我们就可以引用 mylog

在server段中,这样来声明

Nginx允许针对不同的server做不同的Log ,(有的web服务器不支持,如lighttp)

access_log logs/access_8080.log mylog

声明log log位置 log格式

8.日志切割:

#! /bin/bash

LOGPATH=/usr/local/nginx/logs/access.log

BAKPATH=/usr/local/nginx/logs/LOG_BAK/$(date -d yesterday +%Y%m)

mkdir -p $BAKPATH

ak=$BAKPATH/$(date -d yesterday +%Y%m%d).access.log

mv $LOGPATH $bak

touch $LOGPATH

cat /usr/local/nginx/logs/nginx.pid |xargs kill -USR1

上一篇:使用IIS7.0/7.5时配置HttpModules需要注意
下一篇:apache与iis共存