Linux之Nginx入门

Nginx概述

  1、Nginx是一个开源且高性能、可靠的http web服务、代理服务

  2、开源:直接获取源代码

  3、高性能:支持海量开发

  4、可靠:服务稳定

Nginx特点

  1、高性能,高并发

    nginx支持很高的并发,nginx在处理大量并发的情况下比其他web服务要快

  2、轻量且高扩展

    2.1、轻量

      功能模块少,只保留核心模块,其他代码模块化 (易读,便于二次开发,对于开发人员非常友好)

    2.2、高扩展性

      需要什么模块再安装模块,不需要全部安装,并且还支持第三方模块

  3、高可靠性

    3.1、只要不过分几乎不会出现问题

    3.2、其他的web服务需要每隔一段时间进行重启,nginx不需要

    3.3、nginx的宕机时间,是99999级别

  4、支持热部署

    nginx可以再运行期间,更新迭代,代码部署

  5、大多数公司都在用Nginx

    5.1、Nginx技术成熟,具备的功能是企业最常使用而且最需要的

    5.2、适合当前主流架构趋势, 微服务、云架构、中间层

    5.3、统一技术栈, 降低维护成本, 降低技术更新成本

  6、Nginx使用的是Epool网络模型

    6.1、Select: 当用户发起一次请求,select模型就会进行一次遍历扫描,从而导致性能低下。

    6.2、Epoll: 当用户发起一次请求,epoll模型会直接进行处理,效率高效,并无连接限制。

  7、Nginx应用场景

其他的WEB服务

1、apache:httpd,最早期使用的web服务,性能不高,操作难
2、nginx
    tengine:Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性
    openresty-nginx:OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。
3、IIS:windows下的web服务
4、lighttpd:是一个德国人领导的开源 Web 服务器软件,其根本的目的是提供一个专门针对高性能网站,安全、快速、兼容性好并且灵活的 Web Server 环境。具有非常低的内存开销,CPU 占用率低,效能好,以及丰富的模块等特点。
5、GWS:google web server
6、BWS:baidu web server
 
7、Tomcat
8、Resin
9、weblogic
10、Jboss

部署Nginx

  1、下载

官网:https://nginx.org/
软件:https://nginx.org/download/

  2、yum安装

[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
[root@web01 ~]# yum install nginx -y
[root@web01 ~]# systemctl stop httpd
[root@web01 ~]# systemctl start nginx

   3、编译安装

[root@web01 ~]#  wget https://nginx.org/download/nginx-1.20.2.tar.gz
[root@web01 ~]# tar -xf nginx-1.20.2.tar.gz
[root@web01 nginx-1.20.2]# ./configure
[root@web01 nginx-1.20.2]# make
[root@web01 nginx-1.20.2]# make install

平滑增加Nginx模块(使用编译安装的方式才可以)

增加模块必须重新编译。
[root@web01 ~]# tar -xf nginx-1.20.2.tar.gz
[root@web01 ~]# cd nginx-1.20.2
[root@web01 nginx-1.20.2]#./configure  --with-http_ssl_module
[root@web01 nginx-1.20.2]#make 
[root@web01 nginx-1.20.2]#make install 

  报错按照错误缺少的文件直接安装

 

 Nginx的命令

1、-v : 打印版本号
[root@web01 ~]# nginx -v
nginx version: nginx/1.20.2

2、-V : 打印版本号和配置项
[root@web01 ~]# nginx -V
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx 

3、-t : 检查配置文件
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

4、-T : 测试配置文件并启动

5、-q :打印错误日志

6、-s : 操作进程
    stop    :停止
    quit    :退出
    reopen    :重启
    reload    :重载
7、-p : 指定nginx的工作目录
8、-e : 指定错误日志路径
9、-c : 指定配置文件的路径
10、-g : 设置一个全局的Nginx配置项
[root@web01 ~]# nginx -g 'daemon off;'

 Nginx配置文件

  Nginx分为全局配置和模块配置

   全部配置

1、user : 指定Nginx的启动用户
    2、worker_processes : 定义Nginx的worker进程数
        auto === CPU数量
    3、error_log : 错误日志路径
    4、pid : pid的存放文件路径
    5、events : 模块配置
        5.1、worker_connections :每一个worker进程最多同时接入多少个请求
        5.2、use : 指定Nginx的网络模型
    6、http : web服务的模块
        6.1、include : 加载外部的配置项
        6.2、default_type : 如果找不到文件的类型,则按照指定默认类型处理
        6.3、log_format : 定义日志格式
            log_format json '{"@timestamp":"$time_iso8601",'
                  '"host":"$server_addr",'
                  '"service":"nginxTest",'
                  '"trace":"$upstream_http_ctx_transaction_id",'
                  '"log":"log",'
                  '"clientip":"$remote_addr",'
                  '"remote_user":"$remote_user",'
                  '"request":"$request",'
                  '"http_user_agent":"$http_user_agent",'
                  '"size":$body_bytes_sent,'
                  '"responsetime":$request_time,'
                  '"upstreamtime":"$upstream_response_time",'
                  '"upstreamhost":"$upstream_addr",'
                  '"http_host":"$host",'
                  '"url":"$uri",'
                  '"domain":"$host",'
                  '"xff":"$http_x_forwarded_for",'
                  '"referer":"$http_referer",'
                  '"status":"$status"}';
            access_log /var/log/nginx/access.log json ;
        6.4、sendfile : 高效读取文件
        6.5、keepalive_timeout : 长连接保持连接的
            HTTP 1.0 短链接
            HTTP 1.1 长连接
        6.6、server : 网址模块
            6.6.1、listen : 监听的端口
            6.6.2、server_name : 定义域名
            6.6.3、location : 访问路径
                6.6.3.1、root : 指定网址路径
                6.6.3.2、index : 指定网址的索引文件

超级玛丽和象棋小游戏部署

 

 

 

   步骤

1、上传代码

2、编辑配置文件
[root@web01 conf.d]# vim /etc/nginx/conf.d/game.conf 
server {
    listen 80;
    server_name game.test.com;
    location / {
        root /opt/Super_Marie;
        index index.html;
    }
}

3、测试配置文件是否正常
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

4、重启Nginx
[root@web01 conf.d]# systemctl restart nginx 

5、域名解析
C:\Windows\System32\drivers\etc\hosts
172.16.1.7 game.test.com

 

 

 

 

posted @ 2021-12-31 15:42  那就凑个整吧  阅读(83)  评论(0编辑  收藏  举报