目录结构如下:

    Nginx基础知识

        Nginx HTTP服务器的特色及优点

        Nginx的主要企业功能

        Nginx作为web服务器的主要应用场景包括:  

    Nginx的安装

        安装环境

        快速安装命令集合

        各个命令解释

        脚本

        注意

    安装故障总结

        故障一:没有安装pcre或pcre-devel

        故障二:没有安装openssl和openssl-devel

    常用的Nginx http功能模块

    Nginx的目录结构

    Nginx最重要的配置文件nginx.conf详解

    生产中常见的网站状态码

Nginx基础知识:

Nginx HTTP服务器的特色及优点

a. 支持高并发:能支持几万并发连接(特别是静态小文件业务环境)

b. 资源消耗少:在3万并发连接下,开启10个Nginx线程消耗的内存不到200MB

c. 可以做HTTP反向代理及加速缓存,即负载均衡功能,内置对RS节点服务器健康检查功能,这相当于专业的Haproxy软件或LVS的功能

d. 具备Squid等专业缓存软件等的缓存功能

e. 支持异步网络I/O事件模型epoll

Nginx的主要企业功能

a. 使用Nginx运行HTML,JS,CSS,小图片等静态数据(此功能类似Lighttpd软件)

b. Nginx结合FastCGI运行php等动态程序(例如使用fastcgi_pass方式)

c. Nginx结合Tomcat/Resin等支持Java动态程序(常用的proxy_pass)

Nginx作为web服务器的主要应用场景包括:

a. 使用Nginx运行HTML,JS,CSS,小图片等静态数据(此功能类似Lighttpd软件)

b. Nginx结合FastCGI运行php等动态程序(例如使用fastcgi_pass方式)

c. Nginx结合Tomcat/Resin等支持Java动态程序(常用的proxy_pass)

一般情况下普通php引擎支持的并发连接参考为300-1000,Java引擎和数据库的并发连接参考值为300-1500.当然架构不同可能会有浮动

Nginx的安装

安装环境

a. 查看当前系统cat /etc/redhat-release

[root@nginx /]# cat /etc/redhat-release

CentOS release 6.7 (Final)

[root@nginx /]#

b. 查看系统内核uname –r

[root@nginx /]# uname -r

2.6.32-573.el6.x86_64

[root@nginx /]#

快速安装命令集合:

 yum install pcre pcre-devel –y
yum install openssl openssl-devel –y
useradd nginx -M -s /sbin/nologin
./configure --user=nginx --group=nginx --prefix=/application/nginx1.6.2 --with-http_stub_status_module --with-http_ssl_module
make&&make install
ln -s /application/nginx1.6.2/ /application/nginx
/application/nginx/sbin/nginx –t
/application/nginx/sbin/nginx

各个命令解释

a. 安装前需要安装pcre库(兼容正则表达式)

yum install pcre pcre-devel –y

b. 还需要安装openssl

yum install openssl openssl-devel –y

c. 编译之前还需要创建一个用户

useradd nginx -M -s /sbin/nologin

d. 编译安装:

./configure --user=nginx --group=nginx --prefix=/application/nginx1.6.2  --with-http_stub_status_module  --with-http_ssl_module

make&&make install

e. 安装完成后的检查与启动

/application/nginx/sbin/nginx –t

/application/nginx/sbin/nginx

脚本

a. 同样的可以通过脚本实现整体的安装(脚本如下)

 #!/bin/bash
. /etc/init.d/functions nginx_tool_dir=/home/zhaofan/tools
nginx_version=1.6.
nginx_install_dir=/application/nginx$nginx_version
nginx_ln_dir=/application/nginx echo "------step1:install pre and openssl-dvel------"
yum install pcre pcre-devel openssl openssl-devel -y echo "------step2:addd nginx user------"
useradd -s /sbin/nologin -M nginx
sleep echo "------step3:upload nginx software------" mkdir -p $nginx_tool_dir
cd $nginx_tool_dir
[ ! -f nginx-${nginx_version}.tar.gz ] && {
echo "you need to upload packet"
exit
} echo "------step4:install nginx------"
tar xf nginx-$nginx_version.tar.gz
cd nginx-$nginx_version
./configure --user=nginx --group=nginx --prefix=${nginx_install_dir} --with-http_stub_status_module --with-http_ssl_module [ $? -ne ] &&
{
echo "configure is errror"
exit } make && make install
[ $? -ne ] &&
{
echo "make && make install is error"
exit
}
ln -s ${nginx_install_dir} ${nginx_ln_dir} echo "------step5:check and runn nginx------" $nginx_ln_dir/sbin/nginx -t
$nginx_ln_dir/sbin/nginx echo ----------
ps -ef|grep nginx
echo ----------
lsof -i tcp:
echo ----------
curl 127.0.0.1
echo "----------nginx is installed------------"

注意

a. 如果是学习,需要关闭防火墙和selinux,关闭方法如下:

/etc/init.d/iptables stop

setenforce 0临时关闭)

b. 如果想要永久关闭selinux

vi编辑/etc/selinux/config进行下面更改

SELINUX=disabled

c. 也可以通过命令sed直接对命令进行修改

sed -i 's#SELINUX=enable#SELINUX=disabled#g' /etc/selinux/config

按照上述操作启动成功后,通过浏览器打开访问:

安装故障总结

故障一:没有安装pcre或pcre-devel

会提示如下错误:

 ./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

故障二:没有安装openssl和openssl-devel

 ./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.

常用的Nginx http功能模块

Nginx http功能模块

模块说明

Ngx_http_core_module

包括一些核心的http参数配置,对应Nginx的配合为HTTP区块部分

Ngx_http_access_module

访问控制模块,用来控制网站用户对Nginx的访问

Ngx_http_gzip_module

压缩模块,对Nginx返回的数据压缩,属于性能优化模块

Ngx_http_fastcgi_module

FastCGI模块,和动态应用相关的模块,例如PHP

Ngx_http_proxy_module

Proxy 代理模块

Ngx_http_upstream_module

负载均衡模块,可以实现网站的负载均衡功能及节点的健康检查

Ngx_http_rewrite_module

URL地址重写模块

Ngx_http_limit_conn_module

限制用户并发连接数及请求数模块

Ngx_http_limit_req_module

根据定义的key限制Nginx请求过程的速率

Ngx_http_log_module

访问日志模块,以指定的格式记录Nginx客户访问日志等信息

Ngx_http_auth_basic_module

web认证模块,设置web用户通过账号,密码访问Nginx

Ngx_http_ssl_module

ssl模块,用于加密的http连接如https

Ngx_http_stub_status_module

记录Nginx基本访问状态信息等的模块

Nginx的目录结构

|-- client_body_temp

|-- conf                            #这是Nginx所有配置文件的目录

|   |-- fastcgi.conf                 #fastcgi相关参数的配置文件

|   |-- fastcgi.conf.default

|   |-- fastcgi_params               #fastcgi的参数文件

|   |-- fastcgi_params.default

|   |-- koi-utf

|   |-- koi-win

|   |-- mime.types                #媒体类型

|   |-- mime.types.default

|   |-- nginx.conf                  #nginx默认的主配置文件

|   |-- nginx.conf.default

|   |-- scgi_params               #scgi相关参数

|   |-- scgi_params.default

|   |-- uwsgi_params             #uwsgi相关参数

|   |-- uwsgi_params.default

|   `-- win-utf

|-- fastcgi_temp                 #fastcgi临时数据目录

|-- html                              #编译安装Nginx的默认站点目录

|   |-- 50x.html                   #错误页面优雅替代显示文件

|   `-- index.html               #默认的首页文件

|-- logs                             #默认的日志路径包括错误日志和访问日志

|   |-- access.log

|   |-- error.log

|   `-- nginx.pid

|-- proxy_temp                  #临时目录

|-- sbin                             Nginx命令目录

|   `-- nginx                     启动命令

|-- scgi_temp                   #临时目录

`-- uwsgi_temp                  #临时目录

Nginx最重要的配置文件nginx.conf详解

通过命令将nginx配置文件精简化显示(去掉#注释和空行的内容):

egrep -v "#|^$" nginx.conf.default >nginx.conf

worker_processes  1;             #worker进程的数量

events {                         #事件区块的开始

worker_connections  1024;    #每个worker进程支持的最大连接数

}                                #事件区块的结束

http {                           #http区块的开始

include       mime.types;     #nginx支持的媒体类型库文件

default_type  application/octet-stream;  #默认的媒体类型

sendfile        on;           #开启高效传输模式

keepalive_timeout  65;        #连接超时

server {                      #第一个server区块开始,表示一个独虚拟主机站点

listen       80;          #服务端口,默认80

server_name  localhost;   #提供服务的域名主机名

location / {              #第一个location区块开始

root   html;          #站点的根目录,相当于Nginx的安装目录

index  index.html index.htm; #默认的首页文件,如果多个用空格分开

}                         #第一个location区块结束

error_page   500 502 503 504  /50x.html; #出现对象http状态码时使用50x.html回应用户

location = /50x.html {

root   html;

}

}

}                                 #http区块结束

生产中常见的网站状态码

状态码

详细描述说明

200-OK

服务器成功返回网页,这是成功的状态码

301-Moved Permanently

永久跳转,所请求的网页将永久跳转到被设定的新位置

403-Forbidden

禁止访问,虽然这个请求时合法的,但是服务器端因为匹配了预先设置的规则而拒绝相应客户端的请求,此类问题一般为服务器或服务器权限配置不当所致

404-Not Found

服务器找不到客户端请求的指定页面,可能是客户端请求了服务器上不存在的资源所导致

500-Internal Server Error

内部服务器错误,服务器遇到了意料不到的情况,不能完成客户的请求,这是一个较为笼统的报错,一般为服务器的设置或内部程序问题导致

502-Bad Gateway

坏的网关,一般是代理服务器请求后端服务时,后端服务不可用或没有完成相应网关服务器,这通常为反向代理服务器下面的节点出问题导致

503-Service Unavailable

服务当前不可用,可能是服务器超载或停机维护导致的,或者是反向代理没有可以提供的服务节点

504-Gateway Timeout

网关超时,一般是网关代理服务器请求后端服务时,后端服务没有在特定的时间内完成处理请求,多数是服务器过载导致没有在指定的时间内返回数据给前端代理服务器

最新文章

  1. Oracle ------ SQLDeveloper中SQL语句格式化快捷键
  2. [Java入门笔记] Java语言基础(二):常量、变量与数据类型
  3. 关闭Outlook时最小化 dll
  4. highchart 导出图片, 显示空白
  5. Unix网络编程代码 第13章 守护进程和inetd超级服务器
  6. hdu 3722
  7. Java并发专题 带返回结果的批量任务运行 CompletionService ExecutorService.invokeAll
  8. Android适应方案汇总(三)
  9. 导hive表项目总结(未完待续)
  10. 201521123108 《Java程序设计》第2周学习总结
  11. ASP.NET没有魔法——ASP.NET MVC Controller的实例化与执行
  12. Mac 上安装 GCC
  13. CentOS7.2重置root密码的处理方法
  14. hdu6273 线性差分
  15. js+正则+单双引号问题
  16. Java并发程序设计(三) Java内存模型和线程安全
  17. [luogu P1169] [ZJOI2007]棋盘制作
  18. ubuntu的配置文件
  19. solr入门之权重排序方法初探之使用edismax改变权重
  20. 项目管理工具maven(二)

热门文章

  1. 关于UIView的userInteractionEnabled属性
  2. jquery 动画效果插件
  3. 【BZOJ3732】 Network Kruskal+倍增lca
  4. 属性字符串的replaceCharactersInRange方法
  5. GO语言练习:map基本用法
  6. 纪念逝去的岁月——C++实现一个栈(使用类模板)
  7. VS开发好用的扩展
  8. SQL 中怎么查询一个数据库中一共有多少个表
  9. zabbix3.2.0beta2 监控模版
  10. 让你的APP支持iPhone5