web服务部署(二)centos上nginx的安装、配置文件解析

一、一些基础知识

1.1 nginx特点
  • 支持高并发
  • 资源消耗少
  • 可以做http反向代理及加速缓存,也就是负载均衡。(正向代理|反向代理
  • 可以实现squid等专业缓存功能。
  • 支持异步IO模型epoll(同步:select,异步epoll)。
1.2 nginx应用场景
  1. 纯静态网站。
  2. 结合fastcgi运行php动态程序。
  • Linux Nginx Mysql Php。
  • LAMP(LAP、CGI Command Gateway Interface、fastcgi、fpm)
  1. 结合tomcat运行java动态程序(proxy_pass)
  2. 反向代理功能(upstream)
1.3 nginx安装
  1. 在centos系统下

方法1

方法2

  • 安装依赖工具:yum install gcc automake pcre-devel openssl-devel zlib-devel
  • 下载源码包:wget http://nginx.org/download/nginx-1.12.1.tar.gz
  • 进行配置:./configure --prefix=/usr/local/nginx --with-http_ssl_module
  • 安装Makefile编译软件,生成目标代码:make
  • 安装到系统:make install
  1. 通用基本操作
  • 启动:nginx
  • 关闭:nginx -s stop
  • 重启:nginx -s reload
  1. 开机自启
  • 写入rc.local
1.4 nginx目录结构
[root@tysonscloud nginx-1.12.1]# tree /usr/local/nginx/
/usr/local/nginx/
|-- client_body_temp #客户端post一个比较大的文件时,长度超过了nginx缓冲区的大小,需要把这个文件的部分或者全部内容暂存到这里。
|-- conf #nginx所有配置文件的目录。
|   |-- fastcgi.conf #fastcgi相关参数配置文件。
|   |-- fastcgi.conf.default #fastcgi相关参数配置文件的备份
|   |-- fastcgi_params #fastcgi参数文件
|   |-- fastcgi_params.default #fastcgi参数文件的备份
|   |-- koi-utf
|   |-- koi-win
|   |-- mime.types #媒体类型
|   |-- mime.types.default #媒体类型的备份
|   |-- nginx.conf #nginx主配置文件
|   |-- nginx.conf.default #nginx主配置文件的部分
|   |-- scgi_params #scgi相关参数
|   |-- scgi_params.default
|   |-- uwsgi_params #uwsgi相关参数
|   |-- uwsgi_params.default
|   `-- win-utf
|-- fastcgi_temp #fastcgi临时数据目录
|-- html #编译安装nginx时,默认的网页文件存放目录,类似于apache中的/var/www
|   |-- 50x.html #错误替换文件,如果出现502会用此页面内容显示
|   `-- index.html #默认返回的首页文件
|-- logs #nginx默认的日志文件,包括错误日志和访问日志
|   |-- access.log #nginx默认的访问文件
|   `-- error.log #nginx默认的错误文件
|-- proxy_temp #临时文件
|-- sbin
|   `-- nginx #nginx的守护进程
|-- scgi_temp #临时目录
`-- uwsgi_temp #临时目录

二、nginx的配置文件

2.1 nginx的主配置文件(nginx.conf)
[root@tysonscloud nginx-1.12.1]# cat /usr/local/nginx/conf/nginx.conf

#user  nobody; 
#worker进程执行者
worker_processes  1; 
#worker进程数量

#error_log  logs/error.log; #错误日志
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;
#pid文件路径

#每个worker进程支持最大连接数,该区域是nginx的核心功能模块
events {
    worker_connections  1024;
}

#http区块
http {
    include       mime.types;
    #nginx支持的媒体类型
    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  main;
    #默认日志及格式
    
    sendfile        on;
    #开启高效传输模式
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    #连接超时时间
    
    #gzip  on;
    #开启压缩

    #server区块开始,里面定义一个独立的虚拟主机
    server {
        #监听端口
        listen       80;
        #提供服务的域名或主机名
        server_name  localhost;

        #charset koi8-r;
        
        #此虚拟主机访问日志及格式
        #access_log  logs/host.access.log  main;
        
        #location区块
        location / {
            root   html;
            #站的根目录
            index  index.html
            #站的默认首页
            index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #错误状态码为50x时返回/50x.html
        error_page   500 502 503 504  /50x.html;
        #location区块
        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;
        #}
        
        #结合fastcgi实现php动态程序
        # 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.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #新server区块,用于创虚拟主机
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
#http区块结束
2.2 nginx的虚拟主机
  1. 简介
  • 虚拟主机在web服务中心相当于一个独立的网站站点,这个站点一般使用独立的域名或IP或端口,可以独立向外提供web服务。
  1. 虚拟主机类型
  • 基于IP的虚拟主机
  • 基于端口的虚拟主机
  • 基于域名的虚拟主机

例如我们的规划是这样的:

  1. 有个域名
  1. 那么在nginx.conf中,我们应该这样设置。
    server {
                listen 80;
                server_name www.pl1.com;
                error_log logs/error_pl1.log;
                access_log logs/access_pl1.log ;

                location / {
                        root /data/pl1;
                        index index.html;
                }
        }


     server {
                listen 80;
                server_name www.pl2.com;
                error_log logs/error_pl2.log;
                access_log logs/access_pl2.log;

                location / {
                        root /data/pl2;
                        index index.html;
                }
        }
  1. 然后测试配置文件:nginx -t
  2. 重启nginx服务:nginx -s reload
  1. 优化虚拟主机配置
  • include FILE
  1. nginx状态简则模块(http_stub_status_module)
  • 我们通常在主配置文件中这么设置。
  • 然后我们通过这样检测:www.pl1.com/status
server{    
	listen  80;    
	server_name  www.pl1.com;    
	location /status {      
		stub_status on;          #<==打开状态信息开关      
		access_log   off;    
		allow 10.1.1.0/24;
		deny all;
	}  
}
2.3 nginx日志配置
  1. 错误日志
  • error_log FILE LEVEL
  • eg:error_log logs/pl.com_err.log
  1. 访问日志
  • access_log FILE 日志格式名称
  • eg:access_log logs/pl.com_access.log combined
  • 日志格式默认combined
  1. 日志格式定义
  • log_format main '$remote_addr -$remote_user [$time_local] $request'
  • 日志变量说明
Nginx日志变量说明
$remote_addr记录访问网站的客户端地址
$http_x_forwarded_for当前端有代理服务器时,设置web结点记录客户端地址的配置,此参数生效的前提是代理服务器上也进行了相关的x_forwarded_for设置
$remote_user远程客户端用户名称
$time_local记录访问时间与时区
$request用户的http请求起始行信息
$statushttp状态码,记录请求返回的状态
$body_bytes_sents服务器发送给客户端的响应body字节数
$http_referer记录此次请求是从哪个链接访问过来的,可以根据referer进行防盗链设置
$http_user_agent记录客户端访问信息
  1. 日志切割
  • nginx日志逐渐增大,我们编写shell脚本定时切割。
#!/bin/bash
#nginx_log_rotate.sh

Dateformat=`date +%Y%m%d`
Basedir="/usr/local/nginx"
Nginxlogdir="$Basedir/logs"
Logname="access_pl.com"
[ -d $Nginxlogdir ] && cd $Nginxlogdir || exit 1
[ -f ${Logname}.log ] || exit 1
/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log
$Basedir/sbin/nginx -s reload
2.4 nginx的location配置
  1. 语法
  • location [匹配标识] 匹配网站网址(URI){匹配URI后执行的配置段}
  1. 匹配规则(按优先级排序)
  • location = URI:进行普通字符精确匹配
  • location URI:普通字符模糊匹配
  • location ~ URI:波浪线表示执行一个正则匹配,区分大小写
  • location ~* URI:表示执行一个正则匹配,不区分大小写
  • location ^~ URI:表示普通字符匹配,是一种拍他的匹配规则。
  1. 在主配置文件中我们这么写
server {        
	listen       80;        
	server_name  www.pl.com;        
    root   html/www;        
	location / {           
		return 401;        
	}        
	location = / {            
		return 402;        
	}        
	location /documents/ {            
		return 403;      
	}        
	location ^~ /images/ {            
		return 404; 
	}
    location ~* \.(gif|jpg|jpeg)$ {      
		return 500;
	}
}
  • curl -I http://www.pl1.com 402
  • curl -I http://www.pl1.com/ 402
  • curl -I http://www.pl1.com/index.html 401
  • curl -I http://www.pl1.com/documents/xx.html 403
  • curl -I http://www.pl1.com/images/1.gif 404
  • curl -I http://www.pl1.com/documents/q.jpg 500
2.5 nginx rewrite模块
  1. 应用场景
  • 可以调整用户浏览的URL,看起来更规范,更加合乎开发及产品人员的需求。
  • 为了让搜索引擎收录网站内容,并让用户体验更好,企业会将动态URL地址伪装成静态地址提供服务。
  • 网站换新域名后,让旧域名跳转到新的域名上。
  • 根绝特殊变量、目录、客户端的信息进行URL跳转。
  1. 作用
  • rewrite功能用来实现对url地址的重写。
  1. 格式
  • rewrite regx replacement [flag]
  • 其中flag
flag标记符号说明
last本条规则匹配完成后,继续向下匹配新的location URI规则
break本条规则匹配完成即终止,不再匹配后面的任何规则
redirect返回302临时重定向,浏览器地址栏会显示跳转后的URL地址
pemanent返回301永久重定向,浏览器地址栏会显示跳转后的URL地址
  1. 示例
  • rewrite ^/(.*) http://www.newpl.com/$1 permanent
  • $1代表第一个分组。
  • 例如,从www.newpl.com/index.php跳转到http://www.newpl.com/index.php

三、nginx各模块介绍

3.1 模块分类
  1. 核心功能模块(Core Functionality)
  • 主要负责nginx的main区块和event区块,定义了nginx的全局参数
  1. http标准功能模块集合
Nginx http 功能模块模块说明
ngx_http_core_module包括一些核心的http参数配置,对应Nginx的配置为HTTP区块部分
ngx_http_access_module访问控制模块,用来控制网站用户对Nginx的访问
ngx_http_gzip_module压缩模块,对Nginx返回的数据压缩,属于性能优化模块
ngx_http_fastcgi_moduleFastCGI模块,和动态应用相关的模块,例如PHP
ngx_http_proxy_moduleproxy代理模块
ngx_http_upstream_moduleURL地址重写模块
ngx_http_rewrite_moduleURL地址重写模块
ngx_http_limit_conn_module限制用户并发连接数及请求数模块
ngx_http_limit_req_module根据定义的key限制Nginx请求过程的速率
ngx_http_log_module访问日志模块,以指定的格式记录Nginx客户访问日志等信息
ngx_http_auth_basic_moduleWeb认证模块,设置Web用户通过账号、密码访问Nginx
ngx_http_ssl_modulessl模块,用于加密的http连接,如https
ngx_http_stub_status_module记录Nginx基本访问状态信息等的模块
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值