August Rush

一个还在努力成长的小火汁!

游龙当归海,海不迎我自来也。

We create our own demons.

You can reach me at augustrush0923@gmail.com
Nginx初体验
发布:2021年06月28日 | 作者:augustrush | 阅读量: 675

安装Nginx

使用源码编译安装该软件,软件包可以在官方网站http://nginx.org/download下载。

## 安装前置软件
# yum -y install gcc pcre pcre-devel openssl\
>openssl-devel zlib-devel gd gd-devel perl \
>perl-ExtUtils-Embed
# wget http://nginx.org/download/nginx-1.18.0.tar.gz
# tar -zxvf nginx-1.18.0.tar.gz -C /usr/src/
# cd /usr/src/nginx-1.18.0.tar.gz
# ./configure --prefix=/usr/local/nginx \
>--with-ipv6 \
>--with-http_ssl_module \
>--with-http_realip_module \
>--with-http_addition_module \
>--with-http_dav_module \
>--with-http_flv_module \
>--with-http_mp4_module \
>--with-http_gzip_static_module \
>--with-http_perl_module \
>--with-mail \ 
>--with-mail_ssl_module
# make && make install

Nginx采用的是模块化设计,Nginx模块分为内置模块和第三方模块,其中,内置模块中包括主模块与事件模块。

下表给出的模块为默认自动编译的模块,可以使用--without参数禁用。

模块名称 描述 禁用选项
Core Nginx核心功能 --without-http
Access 基于IP的访问控制 --without-http_access_module
Auth Basic HTTP用户认证模块 --without-http_auth_basic_module
Auto Index 自动目录索引 --without-http_autoindex_module
Browser 描述用户代理 --without-http_charset_module
Charset 重新编码网页 --without-http_charset_module
Empty GIF 内存中存放一个图片 --without-http_empty_gif_module
FastCGI FastCGI支持 --without-http_fastcgi_module
Geo 支持IP变量设置 --without-http_geo_module
Gzip Gzip压缩 --without-http_gzip_module
Limit Requests 限制客户端连接频率 --without-http_limit_req_module
Limit Conn 会话的并发连接 --without-http_limit_conn_module
Map 设置变量 --without-http_map_module
Memcached Memcache支持 --without-http_memcached_module
Referer 基于Referer头部信息过滤 --without-http_referer_module
Rewrite 使用正则表达式重写请求 --without-http_rewrite_module
SCGI 支持SCGI协议 --without-http_scgi_module
Upstream 负载金恒 --without-http_upstream_ip_hash_module
Headers 设置http响应头部信息
Index 首页
Log 自定义日志

下表为内置模块中的附加模块,需要在编译时通过--with参数手动开启。

模块名称 描述 开启选项
Embedded Perl 支持Perl --with-http_perl_module
FLV 支持Flash视频 --with-http_flv_module
GeoIP 通过IP变量实现负载均衡 --with-http_geoip_module
Google Perftools 支持谷歌的性能优化工具 --with-google_perftools_module
Gzip Precompression 压缩静态文件 --with-http_gzip_static_module
Image Filter 转换图形的过滤器 --with-http_image_filter_module
MP4 支持MP4 --with-http_mp4_module
Real IP 使用Nginx作为后端服务器 --with-http_realip_module
Secure Link 使用密钥保护页面 --with-http_secure_link_module
SSL 支持HTTPS/SSL --with-http_ssl_module
Stub Status 查看服务器状态 --with-http_stub_status_module
WebDAV 支持WebDAV --with-http_dav_module
Core 邮件代理功能 --with-mail
--with-mail_pop3_module
--with-mail_imap_module
--with-mail_smtp_module
SSL 支持SSL/TLS加密邮件协议 --with-mail_ssl_module

编译第三方模块时可以通过-add-module=/paht/module1编译第三方模块。

Nginx Web服务器软件安装完成后,程序主目录位于/usr/local/nginx/,该目录下的内容分别为conf(主配置文件目录)html(网站根目录)logs(日志文件目录)sbin(主程序目录)

Nginx默认没有提供启动脚本,需要手动输入命令来管理进程,如果需要更加方便地操作服务器进程,建议将常用的进程管理任务写成脚本。下面是Nginx常用的进程管理指令。

# /usr/local/nginx/sbin/nginx  ## 启动主程序
# /usr/local/nginx/sbin/nginx -c \  ## 指定配置文件启动主程序
>/usr/local/nginx/conf/nginx.conf
# /usr/local/nginx/sbin/nginx -s stop  ## 关闭主程序
# /usr/local/nginx/sbin/nginx -s reload  ## 重新加载设置


配置文件解析

Nginx默认的配置文件为/usr/local/nginx/conf/nginx.conf,配置文件主要包括全局eventhttpserver设置

event主要用来定义Nginx工作模式,http提供Web功能,server用来设置虚拟主机,server必须位于http内部,一个配置文件中可以有多个server。

# 设置用户与组
#user  nobody;
# 启动子进程数,可以通过ps -aux | grep nginx查看
worker_processes  1;
# 错误日志文件,以及日志级别
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
# 进程号保存文件
#pid        logs/nginx.pid;


events {
    # 每个进程可以处理的连接数,受系统文件句柄的限制
    worker_connections  1024;
}


http {
    # mime.types为文件类型定义文件
    include       mime.types;
    # 默认文件类型
    default_type  application/octet-stream;

    # 使用log_format可以自定义日志格式,名称为main
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    # 创建访问日志,格式采用main定义的格式
    #access_log  logs/access.log  main;
    # 是否调用sendfile()进行数据复制,sendfile()复制数据是在内核级别完成的,所以会比一般的read、write更高效
    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;
        # 对URL进行匹配
        location / {
            # 设置网页根路径,使用的是相对路径,html指的是处于Nginx安装路径下
            root   html;
            # 首页文件,先找index.html,若没有,再找index.htm
            index  index.html index.htm;
        }
        # 设置错误代码对应的错误页面
        #error_page  404              /404.html;

        # redirect server error pages to the static page /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
        # 下面三行注释行表明,若用户访问URL以.php结尾,则自动将该请求转交给
        # 127.0.0.1服务器,通过proxy_pass可以实现代理功能
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        # 交给127.0.0.1,9000端口 交给fastcgi处理
        #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
        # 拒绝所有的人访问.ht页面
        #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 {
    # 监听TLS使用的443端口
        #    listen       443 ssl;
        #    server_name  localhost;
    # 指定证书文件,使用相对路径证书需要存放在与nginx.conf同目录下
        #    ssl_certificate      cert.pem;
    # 指定私钥文件,使用相对路径私钥需要存放在与nginx.conf同目录下
        #    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+Supervisord+uWSGI+Django1.11.1+Python3.6.5构建

京ICP备20007446号-1 & 豫公网安备 41100202000460号

网站地图 & RSS | Feed