Nginx学习与使用

好久好久之前就知道了Nginx, 不过一来找不到好的教程,二来自己水平有限不理解教程,现在终于感觉效果不错,基本都可以听懂了,来记录笔记!虽说内容不多,但是足够用啦,以后用到更多内容再学习吧.

什么是Web应用服务器?

处理HTTP请求的服务器,返回静态资源.
常见的web应用服务器有哪些?

为什么选择Nginx作为Web应用服务器?

  • 高性能 并发性能好
  • 高扩展性 模块化,可以精简也可以提供复杂的功能
  • 高可用性 三个9,4个9,5个9的可用性
  • 热部署 升级时让用户无感知
  • 应用场景非常多 反向代理,负载均衡,动静分离,缓存,等

安装部署

安装方式?

  • 编译安装 下载源码,自己编译需要的模块
  • yum/apt安装 配置安装源,自动化安装
  • 二进制安装 编译好的程序

编译安装

  1. 下载源代码,解压到执行路径
    wget http://nginx.org/download/nginx-1.25.3.tar.gz
    tar -xf nginx-1.25.3.tar.gz
  2. 安装依赖环境
    yum install openssl-devel pcre-decel gcc
    apt-get install gcc -y
    apt-get install libpcre3 libpcre3-dev -y
    apt-get install zlib1g zlib1g-dev -y
    apt-get install libssl-dev -y
  3. 预编译
    ./configure --prefix=/usr/local/nginx --sbin-path=/bin/ --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module
  4. 添加nginx用户
    useradd nginx -m -s /sbin/nologin
  5. 安装
    make && make install
  6. 查看版本
    /bin/nginx -V
  7. 启动
    /bin/nginx

yum/apt 安装

  1. 配置源
    vi /etc/yum.repos.d/nginx.repo
    [nginx-stable]
    name=nginx stable repo
    baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    gpgcheck=1
    enabled=1
    gpgkey=https://nginx.org/keys/nginx_signing.key
    module_hotfixes=true
    
    [nginx-mainline]
    name=nginx mainline repo
    baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
    gpgcheck=1
    enabled=0
    gpgkey=https://nginx.org/keys/nginx_signing.key
    module_hotfixes=true
  2. 安装
    yum install nginx
    apt-get install nginx

启动与简单配置

  1. 写一个子配置文件
    server{
        listen 80;
        server_name bravexist.cn;
        location / {
            root /html/game;
            index index.html;
        }
    }
  2. 验证配置文件
    nginx -t
  3. 重启Nginx
    nginx -s reload
  4. 模块用途
    http模块    解决用户请求中的报文信息
    server模块  配置具体一个网站的响应操作
    location模块  匹配URI
    # 从上到下依次包含

详细配置

访问控制

拒绝或允许 ip 网段 等

allow all;
deny 1.1.1.1;

文件列表

autoindex on;

用户认证

yum install httpd-tools
htpasswd -c xxx user
auth_basic "some words";
auth_basic_user_file /etc/nginx/htpasswd;

服务状态模块

location = /status {
    stub_status;
}

核心配置

= 精确匹配
~ 大小写敏感
~* 大小写不敏感
^~ 优先级更改,不支持正则匹配

alias别名

location /uri {
    /new_uri;
}
# 类似于重定向

配置网页跳转功能

return 状态码
return url

rewrite

break
rewrite /uri /new_uri break; 跳转时不需要发起新的请求,不重新匹配location

last
rewrite /uri /new_uri last; 跳转时发起新的请求,重新匹配location
permanent 永久重定向
redirect 临时重定向

自签证书

(umask 077;openssl genrsa -out private.pem;)
openssl rsa -in private.pem -pubout -out key.pem

openssl req -new -x509 -key private.pem -out ca.crt -days 36500

配置证书

server{
        listen 443 ssl;
        server_name bravexist.cn;
        root /html/game;
        index index.html;

        ssl_certificate /etc/nginx/pki/ca.crt;
        ssl_certificate_key /etc/nginx/pki/private.pem;
}
server{
        listen 80;
        server_name sg.qiaoxiong.cc;

        location / {
                rewrite (.*) https://bravexist.cn$1;
        }
}

 

 

THE END