• Linux文档整理之【Nginx安装与使用】


    最近继续整理Linux相关文档。这次整理的是Nginx,这里将自己整理的详细文档做个笔记。

    1. 安装环境依赖包

    1、 gcc 语言编译器套件。

    2、 pcre 兼容正则表达式的库 rewrite 模块需要。

    3、 zlib 提供数据压缩函数库 例如gzip压缩。

    4、 openssl 使用https所需的ssl。

    一起安装四个依赖环境包 (如果某些组件已安装可以不用安装)

    yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

    2. 下载和解压安装包

    官网查找最新安装包。 

    http://nginx.org/en/download.html

    下载1.17.1下载地址

    http://nginx.org/download/nginx-1.17.1.tar.gz

    在/usr/local下创建Nginx目录。 (目录可以自定义)

    mkdir nginx

    下载

    wget http://nginx.org/download/nginx-1.17.1.tar.gz

    解压

    tar -xvf nginx-1.17.1.tar.gz

    3. 安装

    切换到安装后的目录。

     cd nginx-1.17.1

    ./configure

    make

    make install

    这几步如果有报错,多数是因为依赖环境没装好比如gcc等,需要重新安装再重复此步骤。

     

    添加到环境变量

    ln -s  /usr/local/nginx/sbin/nginx  /usr/bin

    安装成功后查看版本

    nginx -v

    4. 设置开机启动

    vim /lib/systemd/system/nginx.service

    注意nginx 路径必须为自己安装的路径

    【以下纯文本可以复制】

    [Unit]
    Description=nginx - high performance web server
    Documentation=http://nginx.org/en/docs/
    After=network.target
    [Service]
    Type=forking
    ExecStart=/usr/local/nginx/sbin/nginx
    ExecReload=/usr/local/nginx/sbin/nginx -s reload
    ExecStop=/usr/local/nginx/sbin/nginx -s  stop
    PrivateTmp=true
       
    [Install]  
    WantedBy=multi-user.target

    开机启动

    systemctl enable nginx

    5. Nginx使用与配置

    常用命令

    nginx                      # 运行nginx
    nginx -s reload            # 重新载入配置文件并运行
    nginx -s reopen            # 重启 Nginx
    nginx -s stop              # 停止 Nginx

    运行Nginx

    nginx

    直接输入nginx 没有任何其他提示证明启动成功

    配置文件

    位置(注意自己安装的目录)

    vim /usr/local/nginx/conf/nginx.conf

    默认配置文件内容

    启动nginx后 可以直接通过http://localhost (或者http://自己的ip)访问。查看nginx欢迎页面。

    如果服务器80端口被占用了 那么使用nginx命令时会报错。请修改配置文件里的默认80端口即可。

    配置文件的修改后必须要:nginx –s reload 才能生效。

    静态服务器

    server {
            listen   80;         #监听端口                                 
            server_name  localhost;     #如果绑定了域名 这里填写具体域名    
            client_max_body_size 1024M;  #客户端最大上传文件限制
            location / {
                   autoindex on; //开启目录访问
                   root   /data/wwwroot/webapp;  #站点目录
                   index  index.html;    #首页
           }
    }

    动静分离 

    这里展示通过扩展名分离的方法

    当然还有通过请求分离使用在localtion /static/ {} 等。

    server {
            listen   80;         #监听端口                                 
            server_name  localhost;     #如果绑定了域名 这里填写具体域名   
            #静态数据
            location ~ .(gif|jpg|jpeg|png|bmp|swf|css|js)$     {  
                 root    /data/wwwroot/webapp/html;  
            }  
            #动态请求
            location ~ .(aspx|cshtml)$ {  
                proxy_pass  http://localhost:8080   #动态服务器站点运行地址
            }  
    }

    反向代理

    适合单台服务器应用程序部署,转发

    server {  
            listen       80;                                  
            server_name  localhost;                           
            client_max_body_size 1024M;
            location / {
                proxy_pass http://localhost:8080;  #代理服务器 比如动态应用程序站点
                proxy_set_header Host $host:$server_port;  #请求头信息部分信息一并转发到代理服务器
            }
      }

    均衡负载

    最常用的,适合多台服务器部署应用程序,对外都是同一个域名或站点访问

    # 服务器列表 
    upstream webapp{
            server 192.1681.2:8080 weight=9;  #weight 权重
            server 192.168.1.3:8080 weight=1; 
        }
        server {
            listen       81;                                
            server_name  localhost;                         
            client_max_body_size 1024M;
            location / {
                proxy_pass http://webapp;     #代理指向服务器列表
                proxy_set_header Host $host:$server_port;
                #获取真实客户端访问IP,原理还是将客户端和IP有关的请求头转发到应用服务器
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header REMOTE-HOST $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            }
    }

    配置Https

    https需要依赖openssl包。

    nginx开启SSL模块

    检查自己是否有开启SSL模块

    nginx -V  (大写的V)

    看到有with-http_ssl_module证明已经开启

    如果没有则证明没有开启;以下操作都是针对没有开启with-http_ssl_module的。

    进入自己的安装目录执行:

    ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

    配置完成后执行编译   

    make

    备份已经安装好的nginx (注意自己的安装目录)

    cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

    停止正在运行的nginx

    nginx -s stop

    复制新编译好的nginx覆盖原有nginx

    cp ./objs/nginx /usr/local/nginx/sbin/

    查看是否配置成功

    nginx -V  (大写的V)

    修改站点配置节点

    #server {
       listen       443 ssl;  #监听端口改为443
       server_name  localhost;
    
       ssl_certificate      cert.pem;   #证书prm文件路径
       ssl_certificate_key  cert.key;    #证书key文件路径
    
       ssl_session_cache    shared:SSL:1m;    #设置会话缓存大小
       ssl_session_timeout  5m;     #客户端可以重用会话缓存中ssl参数的过期时间
    
       ssl_ciphers  HIGH:!aNULL:!MD5;    #加密方式  
       ssl_prefer_server_ciphers  on;   #设置加密算法时,优先使用服务端的加密算法
          location / {
             root   html;
             index  index.html index.htm;
        }
     }

    配置Http2

    配置Http2 Nginx 版本必须大于1.10.0以上。Openssl版本必须大于1.0.2。

    在Nginx里面使用Http2必须得使用Https才行。

    可以通过Nginx -V查看目前已安装的版本。

    Http2需要开启with-http_v2_module模块

    配置with-http_v2_module等模块然后make 然后覆盖安装等。 具体不就不再演示了和配置Https一样。

    ./configure --prefix=/usr/share/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module

  • 相关阅读:
    Android Volley入门到精通:定制自己的Request
    Android高效加载大图、多图解决方案,有效避免程序OOM
    Android Volley入门到精通:使用Volley加载网络图片
    Android Volley入门到精通:初识Volley的基本用法
    彻底理解ThreadLocal
    Android中Parcelable接口用法
    Handler详解系列(四)——利用Handler在主线程与子线程之间互发消息,handler详解
    Storm流处理项目案例
    021 使用join()将数组转变为字符串
    020 $.each的使用
  • 原文地址:https://www.cnblogs.com/rui1236/p/11142617.html
Copyright © 2020-2023  润新知