Nginx简介、安装

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


一、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库函数,但通常我们还很有可能使用它的一些功能模块从而需要依赖其他第三方库,如:

  1. gzip模块需要zlib库

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

  2. rewrite模块需要PCRE库

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

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

  3. SSL功能需要OpenSSL库

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

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

二、安装与运行

1. 方式选择

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

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

2. 准备

操作系统:CentOS7.0 64位

Nginx软件 :

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

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. 运行

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

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

    这里写图片描述

  2. /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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Linux上使用Nginx代理静态文件,你需要按照以下步骤进行设置: 1. 首先,你需要安装Nginx。你可以从官方网站下载你所需版本的Nginx压缩包。使用以下命令下载: `wget http://nginx.org/download/nginx-1.16.1.tar.gz` [3] 2. 下载完成后,解压缩压缩包,并进入解压缩后的目录。使用以下命令进行解压缩和进入目录: ``` tar -zxvf nginx-1.16.1.tar.gz cd nginx-1.16.1 ``` 3. 创建Nginx安装目录。使用以下命令创建目录: `mkdir -p /usr/local/nginx` [1] 4. 配置Nginx。使用以下命令进行配置: `./configure --prefix=/usr/local/nginx` [1] 5. 编译和安装Nginx。使用以下命令进行编译和安装: `make && make install` [1] 6. 配置Nginx代理静态文件。打开Nginx的配置文件,并添加以下配置: ``` location ~ .*/(css|js|img) { root /opt/static; } ``` 这表示在`/opt/static`目录下的所有(css/js/img目录下的)资源将由Nginx代理。你可以根据实际情况修改路径和目录名称。保存并退出配置文件。 7. 重新加载Nginx配置。使用以下命令重新加载Nginx配置: `/usr/local/nginx/sbin/nginx -s reload` [2] 现在,Nginx将会代理你指定的Linux静态文件。请确保你的静态文件位于配置文件中指定的路径下,并且文件名和目录结构正确。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Nginx简介,Linux上简单实现部署静态资源、反向代理、负载均衡](https://blog.csdn.net/m0_52765511/article/details/126212249)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [nginx(静态代理)linux版](https://blog.csdn.net/Badman0726/article/details/122024886)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值