Nginx简介、安装

    xiaoxiao2025-01-31  15

    网上已经有很多教程了,这里只是作为自己一个正确操作的记录,并加入一点更加人性化的解释使的过程更好理解;


    一、Nginx

    1. 概述

    Nginx 读作“Engine X”,是一个高性能且轻量级的HTTP和反向代理服务器,也是IMAP/POP3/SMTP服务器。Nginx 是由Igor Sysoev 为俄罗斯访问量第二的Rambler.ru(漫步者)站点开发的。

    上图为 Igor Sysoev

    上图为 Rambler.ru

    Nginx 的源代码是以类BSD(BSD-like)许可证的形式发布的,Nginx 以其稳定性、丰富的功能集、试例配置文件和低系统资源消耗而闻名。它占用内存少,并发能力强,可以替代 Apache Httpd。

    2. 实现

    C语言实现:Nginx 代码完全用C语言写成,已经移植到了许多操作系统,包括类UNIX和Windows系统;

    模块依赖:它的标准模块只需要使用系统的C库函数,但通常我们还很有可能使用它的一些功能模块从而需要依赖其他第三方库,如:

    gzip模块需要zlib库

    zlib是:用于压缩Nginx返回的HTTP响应 - Compresses Responses;

    rewrite模块需要PCRE库

    用于重定向HTTP请求,或者改变请求的URI;

    重写显然需要正则表达式的支持,PCRE是Perl Compatible Regular Expressions的缩写,就是Perl编程语言的一个“兼容正则表达式库”

    SSL功能需要OpenSSL库

    如果要使用SSL,那么需要依赖OpenSSl库;

    另外提下,我的另一篇博文 - 《 Linux、开源软件发展史》的最后部分,有专门的License章节来说明包括BSD在内的许可证的概要。博文 - 《Linux中Apache(httpd)安装、配置、加为服务 》介绍了Apache Httpd ,可以参考。

    二、安装与运行

    1. 方式选择

    Nginx 在一些Linux发行版和 BSD 的各个变种版本的安装包仓库中都会有,可以使用各类系统自带的软件包管理方法安装即可,如CentOS 可以使用yum安装;

    不过最好的方式还是去官网下载最新版本的源码,自己根据需求编译安装,这里就是介绍这种方式;

    2. 准备

    操作系统:CentOS7.0 64位

    Nginx软件 :

    官网下载地址:http://nginx.org/en/download.html;选择版本:nginx-1.10.1.tar.gz;Nginx官网configure参数解释:http://nginx.org/en/docs/configure.html;

    因为要使用gzip、rewrite和ssl,所以还需要准备如下3个依赖包:

    zlib:

    下载地址:http://www.zlib.net/版本:zlib-1.2.8.tar.gz;

    PCRE:

    下载地址:http://www.pcre.org/;版本:pcre-8.38.tar.gz;

    OpenSSL:

    好像所有Linux发行版都已经安装了OpenSSL,所以这个可以不下载;下载地址:http://www.openssl.org/source/版本:openssl-1.1.0-pre6.tar.gz;

    3. 安装

    这里我安装在/apps/nginx中,步骤如下(直接顺序复制粘贴即可):

    [root@localhost sf_source]# mkdir /apps/nginx [root@localhost sf_source]# tar -zxf zlib-1.2.8.tar.gz [root@localhost sf_source]# tar -zxf pcre-8.38.tar.gz [root@localhost sf_source]# tar -zxf nginx-1.10.1.tar.gz [root@localhost openssl-1.1.0-pre6]# cd ../nginx-1.10.1 # 注意下面的zlib、pcre是指定源代码的路径 [root@localhost nginx-1.10.1]# ./configure --prefix=/apps/nginx --with-http_ssl_module --with-zlib=/data/sf_source/zlib-1.2.8 --with-pcre=/data/sf_source/pcre-8.38 [root@localhost nginx-1.10.1]# make && make install

    4. 运行

    执行命令/apps/nginx/sbin/nginx

    访问下你的服务器IP/域名,可以看到:

    /apps/nginx/sbin/nginx可以执行的操作通过-h可以看到,如下:

    [root@localhost sbin]# ./nginx -h nginx version: nginx/1.10.1 Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives] Options: -?,-h : this help -v : show version and exit -V : show version and configure options then exit -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 -p prefix : set prefix path (default: /apps/nginx/) -c filename : set configuration file (default: conf/nginx.conf) -g directives : set global directives out of configuration file

    使用-s 可以stop/quit/reopen/reload

    三、配置文件

    配置文件的官方文档地址:Module ngx_http_core_module

    贴一个我的nginx.conf

    #user nobody; # 工作进程数:一般为CPU核心数 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 { 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 on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; # 定义一个虚拟主机 server { # 监听端口 listen 80; # 此虚拟主机的域名 server_name xxx.com www.xxx.com; #charset koi8-r; #access_log logs/host.access.log main; # 匹配URI location / { # root指定根路径,也就是真实路径是"root + location";可以相对和绝对,这里指定的是相对路径"html" # alias指定别名路径(完全路径),也就是真实路径就是alias; root html; # 指定首页,如果多个,空格隔开 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; } # 我设定files存储一些静态文件 location /files/ { root /data/nginx; } # 我设定图片路径,其下还有/temp/用于存储临时图片 location /images/ { root /data/nginx; } # 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; # } #} }


    转载注明出处:http://blog.csdn.net/u010297957/article/details/52202111

    转载请注明原文地址: https://ju.6miu.com/read-1295952.html
    最新回复(0)