1.pcre及nginx安装包下载 wget

http://www.pcre.org/   pcre用yum安装即可

http://nginx.org/en/download.html

2.安装

-安装所需依赖  yum install -y openssl openssl-devel   gcc*  -y

-创建安装目录/app

-进入/app,  wget  http://nginx.org/en/download.html/nginx2.2.tar.gz

-解压  tar xf nginx2.2.tar.gz

-创建用户 useradd nginx -s /sbin/nologin -M

-安装  cd  /app/ngnix2.2

./configure   --user=nginx   --group=nginx   --prefix=/app/nginx2.2  --with-http_stub_status_module   --with-http_ssl_module

make

make install

-创建软连接

ln -s /app/nginx2.2  /app/nginx

3.安装完成后,检查配置语法

/app/nginx/sbin/nginx -t

出现以下语句为正常

nginx: the configuration file /app/nginx-1.10.3//conf/nginx.conf syntax is ok
nginx: configuration file /app/nginx-1.10.3//conf/nginx.conf test is successful

4.启动服务

/app/nginx/sbin/nginx

5.查看nginx是否启动

[root@localhost nginx]# lsof -i :80

出现以下为正常
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 1495 root 6u IPv4 13304 0t0 TCP *:http (LISTEN)
nginx 1496 nginx 6u IPv4 13304 0t0 TCP *:http (LISTEN)

6.客户端检查服务是否正常可用

linux:wget 127.0.0.1

windows: 浏览器输入127.0.0.1

7.部署一个站点,域名为www.wt.com  与bbs.wt.com

为了使各个子站点分开,将他们放在不同的目录最好,配置也分开

ngnix的网页存放位置:/app/nginx/html目录,在该目录下创建www和bbs两个子目录

/app/nginx/html目录
├── 50x.html:默认错误网页
├── bbs
│   └── index.html
├── index.html
└── www
└── index.html默认网页

8.编写配置文件,/app/ngnix/conf/ngnix.conf是主配置文件,再在该目录下新建一个子目录erxtra,在/app/nginx/conf/extra目录下新建两个bbs.conf,www.conf,status.conf配置文件,

为了安全起见,状态信息页面不要让外部用户访问

.
├── extra
│   ├── bbs.conf
│   ├── status.conf  #ngnix状态监测网页
│   └── www.conf
├── fastcgi.conf
├── fastcgi.conf.default
├── fastcgi_params
├── fastcgi_params.default
├── koi-utf
├── koi-win
├── mime.types
├── mime.types.default
├── nginx.conf
├── nginx.conf.bak
├── nginx.conf.default
├── scgi_params
├── scgi_params.default
├── uwsgi_params
├── uwsgi_params.default
└── win-utf

nginx.conf

#user  nobody;
worker_processes ; #work进程数量 error_log logs/error.log; #错误日志记录 目录为/app/nginx/logs/
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {#事块区
worker_connections ;#每个worker进程支持的最大连接数
}
http { #http区
include mime.types; # nginx支持的媒体类型库文件
include extra/www.conf; #子站点的配置文件
include extra/bbs.conf; #子站点配置文件
include extra/status.conf;#状态监测配置文件
default_type application/octet-stream;#默认的媒体类型
sendfile on;#开启高效传输模式
keepalive_timeout ;#连接超时时间 65秒
}

bbs.conf

    server {
listen ;
server_name bbs.wt.com;#站点别名 location / { #网页存放位置根目录,这里的‘/’根目录是/app/nginx/html
root html/bbs; 存放子站点网页文件的根目录
index index.html index.htm;#首页文件格式
}
error_page /50x.html;#指定错误网页,这里的'/'表示/app/nginx/html
location = /50x.html {
root html;
}
}

www.conf

    server {
listen ;
server_name www.wt.com wt.com;
location / {
root html/www;
index index.html index.htm;
}
error_page /50x.html;
location = /50x.html {
root html;
}
}

status.conf

server {
listen ;
server_name status.wt.com;
location / {
stub_status on;#状态信息开关 打开
access_log off;
}
}

9.配置完后,检查配置语句,并平滑重启

/app/ngnix/sbin/nginx -t  检查配置语句

/app/ngnix/sbin/nginx -s reload  平滑重启

10.浏览器测试

Windows要配置c:/windows/System32/drivers/etc/hosts  来做dns解析

192.168.1.55     www.wt.com    bbs.wt.com  wt.com   status.wt.com

11.日志记录配置,错误日志及访问日志

nginx.conf

需要注意的是:log_format配置必须放在http区块内,否则会出现报错:

#user  nobody;
worker_processes ; error_log logs/error.log;#错误日志记录
error_log /dev/null; #关闭错误日志
events {
worker_connections ;
}
http {
include mime.types;
#######设置访问日志格式,日志格式一定要放在include前边才行,下边的main为日志格式的名称#########
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
include extra/www.conf;
include extra/bbs.conf;
include extra/status.conf;
default_type application/octet-stream;
sendfile on;
keepalive_timeout ; }

日志变量含义:

$server_name:虚拟主机名称。
$remote_addr:远程客户端的IP地址。
-:空白,用一个“-”占位符替代,历史原因导致还存在。
$remote_user:远程客户端用户名称,用于记录浏览者进行身份验证时提供的名字,如登录百度的用户名scq2099yt,如果没有登录就是空白。
[$time_local]:访问的时间与时区,比如18/Jul/::: +,时间信息最后的"+0800"表示服务器所处时区位于UTC之后的8小时。
$request:请求的URI和HTTP协议,这是整个PV日志记录中最有用的信息,记录服务器收到一个什么样的请求
$status:记录请求返回的http状态码,比如成功是200。
$uptream_status:upstream状态,比如成功是200.
$body_bytes_sent:发送给客户端的文件主体内容的大小,比如899,可以将日志每条记录中的这个值累加起来以粗略估计服务器吞吐量。
$http_referer:记录从哪个页面链接访问过来的。
$http_user_agent:客户端浏览器信息
$http_x_forwarded_for:客户端的真实ip,通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。
$ssl_protocol:SSL协议版本,比如TLSv1。
$ssl_cipher:交换数据中的算法,比如RC4-SHA。
$upstream_addr:upstream的地址,即真正提供服务的主机地址。
$request_time:整个请求的总时间。
$upstream_response_time:请求过程中,upstream的响应时间。

extra/bbs.conf     access_log后边的gzip  buffer  flush可以优化高并发下网站访问性能

如果定义了buffer和gzip其中一个参数,日志默认会先写入缓存中,当缓存满了之后,通过gzip压缩缓存中的日志并写入文件,启用了gzip压缩必须保证nginx安装的时候添加了gzip模块。

缓存大小默认为64K。可以配置gzip的1~9的压缩级别,级别越高压缩效率越大,日志文件占用的空间越小,但要求系统性能也越高。默认值是1。

    server {
listen ;
server_name bbs.wt.com; location / {
root html/bbs;
index index.html index.htm;
}
error_page /50x.html;
location = /50x.html {
root html;
}
access_log logs/access_bbs.log main gzip buffer=32k flusk=5s;#只需添加这一句即可,access_bbs.log为日志名,其中(gzip buffer=32k flush=5s为优化高并发下提升
网站访问性能)
}

extra/bbs.conf

    server {
listen ;
server_name wwww.wt.com; location / {
root html/bbs;
index index.html index.htm;
}
error_page /50x.html;
location = /50x.html {
root html;
}
access_log logs/access_www.log main; #指定日志名即可
}

12.配置完后,检查配置语句,并平滑重启

/app/ngnix/sbin/nginx -t  检查配置语句

/app/ngnix/sbin/nginx -s reload  平滑重启

13.日志切割

设置一个定时任务,媒体00点执行/app/nginx/cut_logs.py切割日志

00  00  *  *  *  /usr/bin/python  /app/nginx/cut_logs.py  > /dev/null/  2>&1

脚本内容:

#!/usr/bin/python
# -*- coding:utf-8 -*-
# author:wt import datetime,time
t = time.strftime('%Y-%m-%d',time.localtime(time.time()))
print(t)
try:
sh.mv('/app/nginx/logs/access_www.log','/app/nginx/logs/access_www_%s.log'%t)
except:
p = 1

 14.location  URI匹配

~:正则,用于区分大小写匹配

~*:正则,用于不区分大小写匹配

^~:在做完常规字符串匹配后,不做正则匹配

匹配优先级:

  1."location = / {}"  精确匹配,当用户的请求为空或为“/”时,请求就直接到这个大括号里来

  2."location ^~  /images/  {}"  路径优先匹配,当URI是以/images/开头的路径时,就不再进行正则匹配检查了,请求就直接到这个大括号里

  3."location ~*  \.(gif|jpg|jpeg)$  {}" 正则匹配,当URI是以“.gif或.jpg或.jpeg”结尾的时候,请求就直接到这个大括号里来

  4."location  /doc/  {}"   匹配常规字符串,如果有正则,则优先匹配正则    如: www.wt.com/doc/1.jpg这个URI会直接被nginx分配到3的大括号里去

  5."location  /  {}" 以上所有location都不能匹配上时,就默认把请求分配到这个大括号里

nginx  location配置实例:

cat /app/nginx/conf/extra/www.conf

server {
listen ;
server_name www.wt.com wt.com;
root html/www;
location / {
return ;
# index index.html index.htm;
}
location = / {
return ;
}
location /doc/ {
return ;
}
location ^~ /images {
return ;
}
location ~* \.(gif|jpg|jpeg)$ {
return ;
}
error_page /50x.html;
location = /50x.html {
root html;
}
}

15.nginx重定向 (rewrite)

应用位置:location块、server块、if

语法:

rewrite   正则表达式   要重定向到的地址   flag

注意:

  在根location中(location  /  {})和server块中,flag最好用last,在其他地方最好用break

flag参数:

last:本条规则匹配完后,继续向下匹配新的location URI规则,浏览器地址不变

break:本条规则匹配完成即终止,浏览器地址不变

redirect:返回302临时重定向,浏览器显示跳转后的地址

permanent:返回301永久重定向,浏览器显示跳转后的地址

例子:

访问域名  
www.adc.com/image  自动跳转到  www.adc.com/make/image  
这个如何写

这种需求有多种方法可以实现:
1. 利用Nginx rewrite 内部跳转实现:       
location /image {
          rewrite ^/image/(.*)$     /make/image/$1 last;
}

2.利用alias映射
location  /image  {
       alias  /make/image;  #这里写绝对路径
}

3.利用root映射:
location  /image {
      root   /make;
}

4.利用nginx的permanent 301绝对跳转实现
location /image {
        rewrite ^/image/(.*)$   http://www.adc.com/make/image/$1;
}

5.判断uri实现
if ( $request_uri ~* ^(/image)){
        rewrite ^/image/(.*)$ /make/image/$1 last; 
}

16.Nginx设置访问认证

应用位置:http块、server块、location块、limit_except块

配置两个参数:

  auth_basic      在登录框显示服务器提示的内容

  auth_basic_user_file   存放用户名密码文件的绝对路径

设置访问认证,需要用户名密码:

yum install httpd -y

[root@bogon nginx]# htpasswd -bc conf/htpasswd wt
Adding password for user wt
[root@bogon nginx]# chmod conf/htpasswd
[root@bogon nginx]# chown nginx conf/htpasswd
[root@bogon nginx]# cat conf/htpasswd
wt:5pVjNZUxOKNq2 #生成的密码是密文

配置实例:

[root@bogon nginx]# cat conf/extra/www.conf
server {
listen ;
server_name www.wt.com wt.com;
root html/www;
location / {
# return ;
index index.html index.htm;
auth_basic "wt";
auth_basic_user_file /app/nginx/conf/htpasswd;
}
location = / {
return ;
}
location /doc/ {
return ;
}
location ^~ /images {
return ;
}
location ~* \.(gif|jpg|jpeg)$ {
return 410;
}
error_page /50x.html;
location = /50x.html {
root html;
}
}

17.nginx优化 

http://blog.csdn.net/xifeijian/article/details/20956605

nginx安装及优化的更多相关文章

  1. nginx 的安装、优化、服务器集群

    一.安装 下载地址:http://nginx.org  找到 stable 稳定版 安装准备:nginx 依赖于pcre(正则)库,如果没有安装pcre先安装 yum install pcre pcr ...

  2. zabbix 3.0.3 (nginx)安装过程中的问题排错记录

    特殊注明:安装zabbix 2.4.8和2.4.6遇到2个问题,如下:找了很多解决办法,实在无解,只能换版本,尝试换(2.2.2正常 | 3.0.3正常)都正常,最后决定换3.0.3 1.Error ...

  3. Nginx并发访问优化

    Nginx反向代理并发能力的强弱,直接影响到系统的稳定性.安装Nginx过程,默认配置并不涉及到过多的并发参数,作为产品运行,不得不考虑这些因素.Nginx作为产品运行,官方建议部署到Linux64位 ...

  4. Nginx安装配置PHP(FastCGI)环境的教程

    这篇是Nginx安装配置PHP(FastCGI)环境的教程.Nginx不支持对外部程序的直接调用或者解析,所有的外部程序(包括PHP)必须通过FastCGI接口来调用. 一.什么是 FastCGI F ...

  5. Nginx安装配置与HelloWorld

    <深入理解Nginx>阅读与实践(一):Nginx安装配置与HelloWorld 最近在读陶辉的<深入理解Nginx:模块开发与架构解析>,一是想跟着大牛练练阅读和编写开源代码 ...

  6. nginx安装与应用

    一.nginx的安装与启动: 1.安装依赖库.nginx的一些模块需要依赖其他第三方库,通常有pcre库(perl compatible regular expression,perl兼容正则表达式, ...

  7. Nginx配置性能优化与压力测试webbench【转】

    这一篇我们来说Nginx配置性能优化与压力测试webbench. 基本的 (优化过的)配置 我们将修改的唯一文件是nginx.conf,其中包含Nginx不同模块的所有设置.你应该能够在服务器的/et ...

  8. LNMP环境搭建:Nginx安装、测试与域名配置

    Nginx作为一款优秀的Web Server软件同时也是一款优秀的负载均衡或前端反向代理.缓存服务软件 2.编译安装Nginx (1)安装Nginx依赖函数库pcre pcre为“perl兼容正则表达 ...

  9. nginx安装以及调优

    目录: 1.安装nginx 2.配置nginx 3.调优nginx 4.性能测试 ps:为了方便,文档使用docker容器来操作的. 1.安装nginx 1.1 启动容器.download nginx ...

随机推荐

  1. WeStrom自定义设置修改快捷键

    按照下图操作,不BB: 终极懒人设置:!!!

  2. NSUserDefaults数据存储

    前言 用来保存应用程序设置和属性.用户保存的数据.用户再次打开程序或开机后这些数据仍然存在. 如果往 userDefaults 里存了一个可变数组,取出来的时候这个数组对象就变成了不可变的. NSUs ...

  3. CHNetRequest网络请求

    Paste JSON as Code • quicktype 软件的使用 iOS开发:官方自带的JSON使用 JSON 数据解析 XML 数据解析 Plist 数据解析 NetRequest 网络数据 ...

  4. pycharm自动调整html页面代码缩进不正确的解决办法

    pycharm自动调整html页面代码缩进不正确的解决办法

  5. VS2013安装及破解教程

    https://blog.csdn.net/qq_33742119/article/details/80075352 软件下载的百度云链接,也可以在官网直接下载 链接:https://pan.baid ...

  6. docker 自定义镜像

    step1:自定义镜像 原镜像 registry.aspider.avlyun.org/library/php-apache docker run -d --name xz_apache regist ...

  7. centos6.7安装tomcat

    一.配置环境 安装环境: centos6.7   jdk1.8.0   tomcat8.5 1.到官网下载tomcat 二.下载安装tomcat 1.通过xsheel工具rz命令上传tomcat安装包 ...

  8. 请设计实现一个商城系统开发v2.0【代码优化】

    #!/usr/bin/env python 优化的部分:1.改用字典取键,来调用函数[原来是用if-else判断] [补充]:也可以用列表,按索引取,可以在列表最前面加一个“”任意元素,凑成一个.就和 ...

  9. Javascript 连接两个数组

    JS合并两个数组的方法 我们在项目过程中,有时候会遇到需要将两个数组合并成为一个的情况.比如: var a = [1,2,3]; var b = [4,5,6]; 有两个数组a.b,需求是将两个数组合 ...

  10. BZOJ3065 带插入区间K小值 || 洛谷P4278

    这是一道让我崩溃的题...... 然鹅洛谷上时限被改然后只有20分......好像所有人都被卡了(雾) 由于替罪羊树不是依靠旋转操作而是依靠暴力重构的方式维护树的平衡,所以我们可以考虑使用替罪羊树套区 ...