linux之nginx的安装和基本配置

Nginx介绍

Nginx (engine x) 是一个高性能的 HTTP 和 反向代理 服务,
也是一个IMAP/POP3/SMTP服务。因它的稳定性、丰富的功能集、
示例配置文件和低系统资源的消耗而闻名。
​
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3
)代理服务器,并在一个BSD-like 协议下发行。
其特点是占有内存少,并发能力强,
事实上nginx的并发能力确实在同类型的网页服务器中表现较好
​
在高连接并发的情况下,Nginx是Apache服务器不错的替代品。

优点

Nginx 是一个高性能的 Web 和反向代理服务器, 它具有有很多非常优越的特性:

单机环境下参考服务器配置。 并发连接数在7000+ -8000左右。 集群模式20000+

作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率,这点使 Nginx 尤其受到虚拟主机提供商的欢迎。能够支持高达 50,000 个并发连接数的响应。

作为负载均衡服务器:Nginx 既可以在内部直接支持 Rails 和 PHP,也可以支持作为 HTTP代理服务器 对外进行服务。Nginx 用 C 编写, 不论是系统资源开销还是 CPU 使用效率都比 Perl要好的多。

作为邮件代理服务器: Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last.fm 描述了成功并且美妙的使用经验。

Nginx 安装非常的简单,配置文件 非常简洁(还能够支持perl语法),Bugs非常少的服务器: Nginx 启动特别容易,并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动。你还能够在 不间断服务的情况下进行软件版本的升级。

Nginx安装部署和配置管理

  1. 安装运行环境

    yum install -y zlib zlib-devel openssl openssl-devel pcre pcer-devel wget httpd-tools vim gcc gcc-c++

  2. 下载源代码

    nginx官方网站:nginx news

    阿里nginx网站:The Tengine Web Server

    [root@localhost tmp]# wget https://tengine.taobao.org/download/tengine-2.4.1.tar.gz #阿里下载源

  3. 解压源码压缩包

    [root@localhost tmp]# tar -zxvf tengine-2.4.1.tar.gz
    [root@localhost tengine-2.4.1]# ll
    总用量 404
    -rw-rw-r--  1 root root    931 6月   9 12:47 AUTHORS.te
    drwxrwxr-x  6 root root   4096 6月   9 12:47 auto         检测系统模块依赖
    -rw-rw-r--  1 root root 317572 6月   9 12:47 CHANGES      存放nginx的变化记录日志
    -rw-rw-r--  1 root root  28179 6月   9 12:47 CHANGES.cn
    -rw-rw-r--  1 root root  36025 6月   9 12:47 CHANGES.te
    drwxrwxr-x  2 root root    184 6月   9 12:47 conf         存放nginx主配置文件
    -rwxrwxr-x  1 root root   2502 6月   9 12:47 configure    可执行的脚本,用于存放编译文件
    drwxrwxr-x  4 root root    109 6月   9 12:47 contrib      提供一些好用的插件
    drwxrwxr-x  4 root root     67 6月   9 12:47 docs
    drwxrwxr-x  2 root root     40 6月   9 12:47 html
    -rw-rw-r--  1 root root   1715 6月   9 12:47 LICENSE
    drwxrwxr-x  2 root root     21 6月   9 12:47 man
    drwxrwxr-x 27 root root   4096 6月   9 12:47 modules
    drwxrwxr-x  3 root root     20 6月   9 12:47 packages
    -rw-rw-r--  1 root root   3446 6月   9 12:47 README.markdown
    drwxrwxr-x 10 root root    103 6月   9 12:47 src
    drwxrwxr-x  4 root root     43 6月   9 12:47 tests
    -rw-rw-r--  1 root root     43 6月   9 12:47 THANKS.te
     
  4. 执行编译脚本,配置信息

    [root@localhost tengine-2.4.1]# ./configure --prefix=/tmp/tngx --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-threads --with-file-aio
    --prefix=path
    nginx 程序目录。默认值:/usr/local/nginx。
    ​
    --conf-path=path
    nginx 配置文件的名称。默认值:<prefix>/conf/nginx.conf。但是,在启动时通过 -c filename 指定的配置文件优先级最高。
    ​
    --sbin-path=path
    nginx 可执行文件的名称,仅在安装期间使用。默认值:<prefix>/sbin/nginx。
    ​
    安装后可在配置文件中更改项
    --pid-path=path
    nginx.pid 文件的名称。默认值:<prefix>/logs/nginx.pid。安装后,可以使用 nginx.conf 中的 pid 指令更改。
    ​
    --error-log-path=path
    错误日志文件的名称。默认值:<prefix>/logs/error.log。安装后,可以使用 nginx.conf 中的 error_log 指令更改。
    ​
    --http-log-path=path
    HTTP 服务器的主请求日志文件的名称。默认值:<prefix>/logs/access.log。安装后,可以使用 nginx.conf 中的 access_log 指令更改。
    ​
    --lock-path=path
    为锁定文件的名称设置前缀。默认值:<prefix>/logs/nginx.lock。安装后,可以使用 nginx.conf 中的 lock_file 指令更改。
    ​
    --user=name
    nginx 运行用户。默认值:nobody。安装后,可以使用 nginx.conf 中的 user 指令更改。
    ​
    --group=name
    nginx 运行组。默认值:--user 指定的值。安装后,可以使用 nginx.conf 中的 user 指令更改。
    ​
    --http-client-body-temp-path=path
    定义用于存储包含客户端请求正文的临时文件的目录。默认值:<prefix>/client_body_temp。安装后,可以使用 nginx.conf 中的 client_body_temp_path 指令更改。
    ​
    --http-proxy-temp-path=path
    定义用于存储包含从代理服务器接收到的数据的临时文件。默认值:<prefix>/proxy_temp。安装后,可以使用 nginx.conf 中的 proxy_temp_path 指令更改。
    ​
    --http-fastcgi-temp-path=path
    定义用于存储包含从 FastCGI 服务器接收到的数据的临时文件。默认值:<prefix>/fastcgi_temp。安装后,可以使用 nginx.conf 中的 fastcgi_temp_path 指令更改。
    ​
    --http-uwsgi-temp-path=path
    定义用于存储带有从 uwsgi 服务器接收到的数据的临时文件。默认值:<prefix>/uwsgi_temp。安装后,可以使用 nginx.conf 中的 uwsgi_temp_path 指令更改。
    ​
    --http-scgi-temp-path=path
    定义用于存储包含从 SCGI 服务器接收到的数据的临时文件。默认值:<prefix>/scgi_temp。安装后,可以使用 nginx.conf 中的 scgi_temp_path 指令更改。
    ​
    默认构建的模块
    --without-http_charset_module
    禁用 ngx_http_charset_module 模块,该模块将指定的字符集添加到 “Content-Type” 响应头字段中,还可以将数据从一种字符集转换为另一种字符集,但有一些限制。
    ​
    --without-http_gzip_module
    禁用 ngx_http_gzip_module 模块。
    ​
    --without-http_ssi_module
    禁用 ngx_http_ssi_module 模块。该模块用于处理通过它的响应中的 SSI(服务器端包含)命令。
    ​
    --without-http_userid_module
    禁用 ngx_http_userid_module 模块,该模块设置适用于客户端标识的 cookie。
    ​
    --without-http_access_module
    禁用 ngx_http_access_module 模块,该模块允许限制对某些客户端地址的访问。
    ​
    --without-http_auth_basic_module
    禁用 ngx_http_auth_basic_module 模块,该模块允许通过使用 “HTTP Basic Authentication” 协议验证用户名和密码来限制对资源的访问。
    ​
    --without-http_mirror_module
    禁用 ngx_http_mirror_module 模块。
    ​
    --without-http_autoindex_module
    禁用 ngx_http_autoindex_module 模块。该模块处理以斜杠字符 “/” 结尾的请求,并生成目录列表。
    ​
    --without-http_geo_module
    禁用 ngx_http_geo_module 模块,该模块使用取决于客户端 IP 地址的值来创建变量。
    ​
    --without-http_map_module
    禁用 ngx_http_map_module 模块,该模块创建的变量的值取决于其他变量的值。
    ​
    --without-http_split_clients_module
    禁用 ngx_http_split_clients_module 模块,该模块创建用于 A/B 测试的变量。
    ​
    --without-http_referer_module
    禁用 ngx_http_referer_module 模块,该模块可以阻止对 “Referer” 标头字段中具有无效值的请求的站点访问。
    ​
    --without-http_rewrite_module
    禁用 URL 转发(rewrite)。
    ​
    --without-http_proxy_module
    禁用 HTTP 服务器代理(proxy)模块。
    ​
    --without-http_fastcgi_module
    禁用 ngx_http_fastcgi_module 模块。该模块允许将请求传递到 FastCGI 服务器。
    ​
    --without-http_uwsgi_module
    禁用 ngx_http_uwsgi_module 模块。该模块允许将请求传递到 uwsgi 服务器。
    ​
    --without-http_scgi_module
    禁用 ngx_http_scgi_module 模块。该模块允许将请求传递到 SCGI 服务器。
    ​
    --without-http_grpc_module
    禁用 ngx_http_grpc_module 模块。该模块允许将请求传递到 gRPC 服务器。
    ​
    --without-http_memcached_module
    禁用 ngx_http_memcached_module 模块,该模块用于从内存缓存服务器(memcached)获取响应。
    ​
    --without-http_limit_conn_module
    禁用 ngx_http_limit_conn_module 模块,该模块限制每个键的连接数,特别是来自单个 IP 地址的连接数。
    ​
    --without-http_limit_req_module
    禁用 ngx_http_limit_req_module 模块,该模块用于限制每一个定义的密钥的请求的处理速率,特别是从一个单一的 IP 地址的请求的处理速率。
    ​
    --without-http_empty_gif_module
    禁用生成发射单像素透明 GIF 的模块。
    ​
    --without-http_browser_module
    禁用 ngx_http_browser_module 模块,该模块创建变量,创建变量,其值取决于 “User-Agent” 请求标头字段的值。
    ​
    --without-http_upstream_hash_module
    禁用 hash 负载平衡方法的模块。
    ​
    --without-http_upstream_ip_hash_module
    禁用 ip_hash 负载平衡方法的模块。
    ​
    --without-http_upstream_least_conn_module
    禁用 least_conn 负载平衡方法的模块。
    ​
    --without-http_upstream_keepalive_module
    禁用提供到 upstream 内服务器连接缓存的模块。
    ​
    --without-http_upstream_zone_module
    禁用可以将 upstream 的运行时状态存储在共享内存区域中的模块。
    ​
    --without-http
    禁用 HTTP 服务器。
    ​
    --without-http-cache
    禁用 HTTP 缓存。
    --with-select_module
    启用 select() 方法的模块。
    ​
    --with-poll_module
    启用 poll() 方法的模块。
    ​
    --with-threads
    启用线程池的使用。
    ​
    --with-file-aio
    启用异步文件I/O(AIO)。
    ​
    --with-http_ssl_module
    启用 HTTPS 协议支持,需要 OpenSSL 库。默认情况下未构建此模块。
    ​
    --with-openssl=path
    设置 OpenSSL 库源的路径。
    ​
    --with-openssl-opt=parameters
    为 OpenSSL 设置其他构建选项。
    ​
    --with-http_v2_module
    启用 HTTP/2 协议支持。默认情况下未构建此模块。
    ​
    --with-http_realip_module
    启用 ngx_http_realip_module 模块的功能,该模块将客户端地址更改为在指定的 "header " 字段中发送的地址。默认情况下未构建此模块。
    ​
    --with-http_addition_module
    启用 ngx_http_addition_module 模块,该模块可在响应之前和之后添加文本。默认情况下未构建此模块。
    ​
    --with-http_sub_module
    启用 ngx_http_sub_module 模块,该模块通过将一个指定的字符串替换为另一个指定的字符串来修改响应。默认情况下未构建此模块。
    ​
    --with-http_dav_module
    启用 ngx_http_dav_module 模块,该模块通过 WebDAV 协议提供文件管理自动化。默认情况下未构建此模块。
    ​
    --with-http_flv_module
    启用 ngx_http_flv_module 模块,该模块为 Flash Video (FLV) 文件提供伪流服务器端支持。默认情况下未构建此模块。
    ​
    --with-http_mp4_module
    启用 ngx_http_mp4_module 模块,该模块为 MP4 文件提供伪流服务器端支持。默认情况下未构建此模块。
    ​
    --with-http_gunzip_module
    对于不支持 “gzip” 的客户端,启用 ngx_http_gunzip_module 模块,使用 “Content-Encoding: gzip” 解压缩响应。默认情况下未构建此模块。
    ​
    --with-http_gzip_static_module
    启用 ngx_http_gzip_static_module 模块,该模块支持发送扩展名为 “.gz” 的预压缩文件,而不是常规文件。默认情况下未构建此模块。
    ​
    --with-http_auth_request_module
    启用 ngx_http_auth_request_module 模块,该模块基于子请求的结果实现客户端授权。默认情况下未构建此模块。
    ​
    --with-http_random_index_module
    启用 ngx_http_random_index_module 模块,该模块处理以斜杠 “/” 结尾的请求,并在目录中选择一个随机文件作为索引文件。默认情况下未构建此模块。
    ​
    --with-http_secure_link_module
    启用 ngx_http_secure_link_module 模块。该模块用于检查请求链接的真实性,保护资源不受未经授权的访问,并限制链接生存期。默认情况下未构建此模块。
    ​
    --with-http_degradation_module
    启用 ngx_http_degradation_module 模块。默认情况下未构建此模块。
    ​
    --with-http_slice_module
    启用 ngx_http_slice_module 模块,该模块将请求拆分为多个子请求,每个子请求都返回一定范围的响应。可为大响应提供更有效的缓存。默认情况下未构建此模块。
    ​
    --with-http_stub_status_module
    启用 ngx_http_stub_status_module 模块,该模块提供对基本状态信息的访问。默认情况下未构建此模块。
    ​
    --with-http_xslt_module
    --with-http_xslt_module=dynamic
    启用 ngx_http_xslt_module 模块,该模块是一个过滤器,可使用一个或多个 XSLT 样式表转换 XML 响应。构建和运行此模块需要 libxml2 和 libxslt 库。默认情况下未构建此模块。
    ​
    --with-http_image_filter_module
    --with-http_image_filter_module=dynamic
    启用 ngx_http_image_filter_module 模块,该模块可以转换 JPEG,GIF,PNG 和 WebP 格式的图像。构建和运行此模块需要 libgd 库。默认情况下未构建此模块。
    ​
    --with-http_geoip_module
    --with-http_geoip_module=dynamic
    启用 ngx_http_geoip_module 模块,该模块根据客户端 IP 地址和预编译的 MaxMind 数据库创建变量。默认情况下未构建此模块。
    ​
    --with-google_perftools_module
    启用 ngx_google_perftools_module 模块,以使用 Google Performance Tools 对 nginx 性能分析。该模块供 ngin 开发人员使用。默认情况下未构建。
    ​
    --with-cpp_test_module
    启用 ngx_cpp_test_module 模块。
    ​
    --with-compat
    启用动态模块兼容性。
    ​
    --with-cc=path
    设置 C 编译器的名称。
    ​
    --with-cpp=path
    设置 C 预处理器的名称。
    ​
    --with-cc-opt=parameters
    设置将添加到 CFLAGS 变量的其他参数。在 FreeBSD 下使用系统 PCRE 库时,应指定

  5. 编译安装

    [root@localhost tengine-2.4.1]# make && make install
    查看目录
    [root@localhost tngx]# ll
    总用量 0
    drwxr-xr-x 2 root root 333 11月 22 21:24 conf   存放配置文件
    drwxr-xr-x 2 root root  40 11月 22 21:24 html   存放网页根目录文件以及静态资源
    drwxr-xr-x 2 root root   6 11月 22 21:24 logs   存放日志文件
    drwxr-xr-x 2 root root  19 11月 22 21:24 sbin   存放可执行文件(命令)
  6. 开启nginx

    进入sbin目录下
    [root@localhost sbin]# ./nginx   #开启nginx
    
    [root@localhost sbin]# ./nginx -s stop  #关闭nginx
    
    [root@localhost sbin]# ./nginx -s reload  #平滑重启
  7. 浏览器访问

    输入地址 http://虚拟机ip

    注意保证防火墙关闭
  8. 配置nginx全局可用

    [root@localhost sbin]# ln -s /tmp/tngx/sbin/nginx /usr/sbin/
    !!!慎用,有坑

Nginx命令

[root@localhost /]# nginx -h
Tengine version: Tengine/2.4.1
nginx version: nginx/1.22.1
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
             [-e filename] [-c filename] [-g directives]

Options:
  -?,-h         : this help  #输出帮助文档
  -v            : show version and exit  #查看nginx的版本
  -V            : show version and configure options then exit #查看版本和nginx的配置选项
  -t            : test configuration and exit  #测试配置文件的正确性
  -T            : test configuration, dump it and exit  #测试配置文件,并显示配置文件(这个命令可以快速查看配置文件)
  -q            : suppress non-error messages during configuration testing #测试配置文件,但是只显示错误信息
  -s signal     : send signal to a master process: stop, quit, reopen, reload 
  #发送信号,stop 强制关闭  quit平滑关闭 reopen重新记录日志  reload重读配置文件   
  -p prefix     : set prefix path (default: /tmp/tngx/) #设置前缀
  -e filename   : set error log file (default: logs/error.log) #设置报错日志文件
  -c filename   : set configuration file (default: conf/nginx.conf)  #设置配置文件
  -g directives : set global directives out of configuration file  #附加配置文件路径

  -m            : show all modules and exit  #展示所有模块
  -l            : show all directives and exit  #展示目录

Nginx信号传递

实现配置文件重读--以gsc用户开启nginx工作进程

查看当前nginx进程
[root@localhost /]# ps aux | grep nginx
发现当前工作进程由nobody开启

创建gsc用户
[root@localhost /]# useradd gsc
修改nginx配置文件,位置在 nginx目录/conf/nginx.conf

让nginx重读配置文件
[root@localhost /]# nginx -s reload
重新查看进程
 

信号传递原理

nginx -s 参数对应的信号

参数     信号     作用
stop    TERM   强制关闭nginx
null     INT   强制关闭整个nginx服务
quit    QUIT   平缓关闭整个nginx服务
reopen  USR1   重新打开日志记录
reload  HUB    重新读取配置,平滑退出旧的配置

Nginx日志文件

nginx日志默认存放在 ningx包下的logs文件中,也可以在配置文件中手动配置,配置文件存放在nginx的conf包下的nginx.conf

nginx日志分割

#将原日志文件重命名
[root@localhost logs]# mv access.log "access.log_$(date +"%Y-%m-%d")" 

#使nginx重新加载
[root@localhost logs]# /tmp/tngx/sbin/nginx -s reopen

自动日志切割

  1. 编写日志分割脚本

    #!/bin/bash
    logs_path="/tmp/tngx/logs/"
    mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")
    mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y-%m-%d").log
    
    kill -USR1 `cat /tmp/tngx/logs/nginx.pid`
  2. 执行脚本

    [root@localhost 11]# bash /opt/cut_nginx_log.sh 
  3. 设置定时执行

    [root@localhost 11]# crontab -e
    
    0 0 * * *  /bin/bash /opt/cut_nginx_log.sh # 设置每天零点执行
    ~                                                                                                          
    ~                            

Nginx单虚拟主机配置

Nginx配置文件

基本语法

worker_processes  1;  #全局指令,在整个nginx中有效
events {
    worker_connections  1024; #括号内为局部指令,只对当前括号内的内容有效
}
#user  nobody;         #nginx启动用户
worker_processes  1;   #nginx工作进程数

#error_log  logs/error.log  info;   #nginx错误日志记录位置

#pid        logs/nginx.pid;         #nginx进程日志记录位置


events {                            #nginx工作进程配置
    use epoll;                      #使用epoll的I/O 模型  
    worker_connections 20000;	    #工作进程的最大连接数量
    client_header_buffer_size 4k;   #客户端请求头部的缓冲区大小
    open_file_cache max=2000 inactive=60s; #为打开文件指定缓存,默认是没有启用的,max指定缓存最大数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存 
    open_file_cache_valid 60s;             #这个是指多长时间检查一次缓存的有效信息。
    open_file_cache_min_uses 1;            #open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如果有一个文件在inactive时间内一次没被使用,它将被移除。

}


http {
    include       mime.types;                  #引入其他文件
    default_type  application/octet-stream;    #默认文件类型,默认为text/plain

	#指定日志格式
    #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;  #用来提升网络包的传输'效率',sendfile开启才可开启

    #keepalive_timeout  0;
    keepalive_timeout  65;  #连接保持时间

    #gzip  on;             #文件压缩模式

    server {			  #一个服务模块
        listen       80;   #占用端口号
        server_name  localhost; #虚拟服务器的识别标志

        #charset koi8-r;    #设置编码集

		#该服务模块的日志位置
        #access_log  logs/host.access.log  main;
        
		#请求路径匹配
        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html; #错误页面展示
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

nginx静态网页搭建

#创建文件夹
[root@localhost 11]# mkdir -p /baizhi
#存放一些网页
#在nginx中添加配置
[root@localhost /]# vi /tmp/tngx/conf/nginx.conf


#service修改为如下配置信息
server {
    listen     9999;
    charset utf-8;
    location /{
        root /baizhi;
    }
}
#检查配置文件是否正常
[root@localhost baizhi]# /tmp/tngx/sbin/nginx
#重启nginx
[root@localhost baizhi]# /tmp/tngx/sbin/nginx -s reload

访问指定路径

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值