Nginx:小白进阶篇

前言

一直对nginx处于简单应用的状态,为了更深入理解nginx,这里对nginx进行一些总结,同时分享一下个人理解,若有错误请指出,话不多说,直入正题。

解压源码

先下载源码文件,了解一下源码,点击进入 nginx 下载页面,第二列是linux版本源码,第三列是windows源码。

linux版本源码目录

├──.
├── auto├── .	//目录中存放了大量脚本文件(自动检测系统环境以及编译相关的脚本),和configure脚本程序有关
│		├── cc //关于编译器相关的编译选项的检测脚本
│		├── lib	//nginx编译所需要的一些库的检测脚本
│		├── os	//与平台相关的一些系统参数与系统调用相关的检测
│		├── types	//与数据类型相关的一些辅助脚本
│		└── ……	//其他文件
├── CHANGES	//nginx提供的特性说明,英文版本
├── CHANGES.ru	//nginx提供的特性说明,俄文版本
├── conf	//存放一些默认配置文件和特性文件,在make install后,默认配置文件会拷贝到安装目录中去
├── configure	//Nginx的自动脚本程序
├── contrib	//存放一些实用工具,如geo配置生成工具(geo2nginx.pl)
├── html	//html目录中存放两个静态网页文件:50x.html(错误提示页面)和index.html(nginx欢迎页面)
├── LICENSE	//开源协议许可说明
├── man //目录中存放了Nginx软件的帮助文档,默认配置文件,在make install完成后,会拷贝到安装目录中去,在命令行中可以使用man命令查看,如 man nginx
├── README	//源码说明文本
└── src	//存放nginx的源代码
	├── core        nginx的核心源代码,包括常用数据结构的定义,以及nginx初始化运行的核心代码如main函数
    ├── event       对系统事件处理机制的封装,以及定时器的实现相关代码
    │   └── modules 不同事件处理方式的模块化,如select、poll、epoll、kqueue等
    ├── http        nginx作为http服务器相关的代码
    │   └── modules 包含http的各种功能模块
    ├── mail        nginx作为邮件代理服务器相关的代码
    ├── misc        一些辅助代码,测试c++头的兼容性,以及对google_perftools的支持
    └── os          主要是对各种不同体系统结构所提供的系统函数的封装,对外提供统一的系统调用接口

windows版本源码目录

├── .
├── conf	//存放一些默认配置文件和特性文件,在make install后,默认配置文件会拷贝到安装目录中去
├── contrib	//存放一些实用工具,如geo配置生成工具(geo2nginx.pl)
├── dosc	//存放一些文稿,把一些文本集中放到这里	
├── html	//html目录中存放两个静态网页文件:50x.html(错误提示页面)和index.html(nginx欢迎页面)
├── logs	//默认日志存放目录
├── nginx.exe	//nginx可执行文件
└── temp	//默认临时文件存放目录

快速安装

linux安装

yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel		//安装依赖,没有依赖的话执行配置时会提示错误
tar -zxvf nginx-1.19.2.tar.gz	//解压源码
cd nginx-1.19.2		//进入解压目录
./configure		//执行配置
make && make install	//编译和安装(默认安装在/usr/local/nginx)

以上是linux下快速安装nginx文件的命令,若权限不够,可以在执行的命令前面加上sudo。安装完成之后便可以直接访问IP地址进行验证,若页面返回下面提示,表示安装成功。

Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

若出现错误,请检查防火墙80端口是否开启,若没有开放,需要进行处理。

  • 方法一,nginx默认监听80端口,配置iptables规则开放80端口。
vim /etc/sysconfig/iptables	//编辑防火墙配置文件
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT	//在文件中间添加iptables规则
service iptables restart	//重启防火墙
  • 方法二,关闭iptables规则
iptables -F && iptables -t nat -F 

重新访问ip请求测试。

windows安装

直接解压,直接启动nginx.exe程序(未测试)

nginx.conf 配置说明

安装完成之后,我们现在来详细了解一下nginx的配置文件。
先贴出nginx.conf的默认配置内容,直接在内容上方进行注释说明。

#运行的用户为nobody;第二个可选参数是运行的用户组为nobody,可省略
#user  nobody nobody;

#启动进程,通常设置成和cpu的数量相等
worker_processes  1;

#全局错误日志位置,第二个参数是日志级别
#error_log  logs/error.log;	
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#PID文件,记录当前启动的nginx的进程ID
#pid        logs/nginx.pid;

#工作模式及连接数上限
events {
	#单个后台worker process进程的最大并发链接数
    worker_connections  1024;
}

#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
	#设定mime类型(邮件支持类型),类型由mime.types文件定义
    include       mime.types;
    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 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
    sendfile        on;
    #tcp_nopush     on;
	
	#连接超时时间
    #keepalive_timeout  0;
    keepalive_timeout  65;

	#gzip压缩开关
    #gzip  on;

	#设定实际的服务器列表
	#upstream zp_server1{
    #   server 127.0.0.1:8089;
   	#}
   	
	#HTTP服务器配置
    server {
    	#监听80端口,80端口是知名端口号,用于HTTP协议
        listen       80;
        #定义使用localhost访问,若要测试未解析的www.xxx.com域名,则需要在系统的host文件中对ip和域名进行本地解析
        server_name  localhost;
		#编码格式
        #charset koi8-r;

		#请求记录成功日志位置
        #access_log  logs/host.access.log  main;

        location / {
        	#指向网站的入口目录
            root   html;
            #首页
            index  index.html index.htm;
        }
        
    ## 错误处理页面(可选择性配置)
		#将服务器错误404重定向到静态页
        #error_page  404              /404.html;
        #将服务器错误500 502 503 504 重定向到静态页/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.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 {
    #    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;
    #    }
    #}

}

配置完毕之后,需要重载配置文件,重载之前可通过命令名先测试文件中有无语法错误

./nginx -t		//测试配置文件是否有语法错误
./nginx -s reload  //重载配置

FAST_CGI

介绍:
CIG(公共网关接口),HTTP服务器与其他程序进行‘交谈’的工具;客户端的每次请求都必须通过CGI解释器,CGI解释器每次运行必须重新解析php.ini,重新载入全部扩展并重初始化全部数据结构。当处理完请求把结果返回给服务器后便退出php-cgi,下次请求需要重新解析php.ini。
fast_cgi:客户端的请求到达服务器后,启动fast_cig进程管理器,解析php.ini,载入全部扩展并重初始化全部数据结构。同时把请求分配到子进程php-cgi,处理完之后,结果返回给进程管理器,进程管理器返回给服务器,该请求的子进程关闭,进程管理依旧依旧维持运行,下次请求再次分配一个php-cgi子进程进行处理,不需要重新解析php.ini和初始化。好处:避免频繁初始化,可以持续连接,管理多个cgi。

FAST_CGI可在http, server, location这些代码块中配置。

一些配置

  • fastcgi_pass address; #配置地址;
    可用地址:location, if in location
    示例:fastcgi_pass localhost:9000;
  • fastcgi_index filename; #fastcig默认的主页资源
    可用地址:location, if in location
    示例:
	fastcgi_index index.php;
  • fastcgi_param parameter value [if_not_empty]; #设置传递给FastCGI服务器的参数值,可以是文本,变量或组合
    可用地址:location, if in location
    示例:
	fstcgi_param	SCRIPT_FILENAME/app/php$fastcgi_script_name;
	include fastcgi_params;

未完待续……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值