NGINX 入门

NGINX下载地址

下载地址

NGINX介绍

NGINX是一个高性能的Web服务器和反向代理服务器,通常也被称为反向代理服务器。它是一个开源的软件,由俄罗斯程序员Igor Sysoev编写,最初是为Rambler.ru(俄罗斯最大的门户网站)开发的。NGINX的主要特点包括高并发性、高性能、可扩展性、稳定性以及易于配置和使用。

NGINX特点

高并发性、高性能、可扩展性、稳定性以及易于配置和使用。

NGINX的主要功能

动静分离、web服务器、反向代理、负载均衡、SSL/TLS加密、缓存等

NGINX配置文件

nginx的配置文件是一个纯文本文件,它一般位于nginx安装目录的conf目录下,整个配置文件是以block的形式组织的。 每个block一般以一组大括号“{}”来表示,block可以分为以下几个层次:main块、events块、http块等,而在http块中又包含有server块,即server block,server block 中又可分为location块,并且一个server block中可以包含多个location block。

全局块

从配置文件开始到events块之间的内容为全局块,主要设置一些影响nginx服务器整体运行的配置指令。

    user:用于指定Nginx运行的用户和用户组
    worker_processes:用于指定Nginx的工作进程数  一般设置为cpu核心数的数量
    pid:nginx运行进程id
    error_log:错误日志
events块

用于控制nginx的网络连接和事件处理。

    worker_connections:定义了nginx可以同时处理的最大连接数。
    multi_accept:是否允许同时接受多个连接。
    accept_mutex:控制接受连接的互斥锁。
    accept_mutex_delay:设置互斥锁的延迟时间。
http块

用于配置协议级别、配置负载均衡、配置server及配置匹配请求路径。

    include:在http块中引入其他配置文件
    default_type:application/octet-stream   #text/plain 等
    sendfile:用于实现高效的文件传输
    keepalive_timeout:用于指定HTTP连接的保持时间,减少TCP连接的数量
    keepalive_requests:设置长连接最大请求数
    gzip:以减少传输的数据,提高网页加载速度
    server块:创建虚拟服务主机
    upstream块:主要用于负载均衡,设置一系列的后端服务器。
    location块:用于匹配网页位置
    等等

NGINX正向代理

正向代理的一个典型应用是绕过访问限制,比如局域网内主机,通过一个代理服务器访问外网服务,则可以使用正向代理来模拟客户的ip和地址从而绕过局域网的限制。

在Nginx中,可以通过配置proxy模块来实现正向代理。下面是一个简单的Nginx配置示例,用于实现正向代理:

server {  
    listen 80;  
    server_name chencs.com;  
    location / {  
        proxy_pass http://a.chencs.top;  
    }  
}

NGINX反向代理

作为客户端和目标服务器之间的中间代理,隐藏了目标服务器的真实IP地址,并且对客户端请求进行转发和过滤。

用途:负载均衡、缓存、SSL加密和安全

server {  
    listen 80;  
    server_name chencs.com;  
    location / {  
        proxy_pass http://a.chencs.top;  
        proxy_set_header Host $host;  
        proxy_set_header X-Real-IP $remote_addr;  
    }  
}

NGINX负载均衡

将请求分发到多个后端服务器上,Nginx的负载均衡功能基于轮询、IP哈希、URL哈希等算法来实现,可以有效地平衡服务器的负载,提高系统的性能和可伸缩性。

    轮询方式:
    http {
        upstream polling {  
            server polling1.com;  
            server polling2.com;  
            server polling3.com;  
        }  
        server {  
            location / {  
                proxy_pass http://polling;  
            }  
        }
    }  

    ip哈希方式:
    http {  
        upstream iphash {  
            server iphash1.com;  
            server iphash2.com;  
            server iphash3.com;  
            ip_hash;  
        }  
        server {  
            location / {  
                proxy_pass http://iphash;  
            }  
        } 
    }

    URL哈希
    http {  
        upstream urlhash {  
            server urlhash1.com;  
            server urlhash2.com;  
            server urlhash3.com;  
            hash $request_uri;
        }  
        server {  
            location / {  
                proxy_pass http://urlhash;  
            }  
        } 
    }

NGINX缓存

缓存是Nginx的一个重要功能,它可以提高网站的性能和响应速度。Nginx提供了多种缓存方式,包括代理缓存、FastCGI缓存和静态文件缓存等。

代理缓存

http {  
    proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;  
    server {  
        listen 80;  
        server_name example.com;  
        
        location / {  
        proxy_pass http://backend_server;  
        proxy_cache my_cache;  
        proxy_cache_valid 200 304 12h;  
        proxy_cache_valid any 1h;  
        proxy_cache_key $scheme$proxy_host$request_uri;  
        proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;  
        proxy_ignore_headers Cache-Control Expires Set-Cookie;  
        }  
    }  
}

FastCGI缓存


http {  
  fastcgi_cache_path /path/to/cache levels=1:2 keys_zone=fastcgi_cache:10m inactive=60m max_size=1g;  
  server {  
    listen 80;  
    server_name example.com;  
  
    location ~ \.php$ {  
      fastcgi_pass unix:/var/run/php-fpm.sock;  
      fastcgi_index index.php;  
      fastcgi_cache fastcgi_cache;  
      fastcgi_cache_valid 200 304 12h;  
      fastcgi_cache_valid any 1h;  
      fastcgi_cache_key $scheme$proxy_host$request_uri;  
      fastcgi_cache_use_stale error timeout invalid_header updating http_500 http_503;  
      include fastcgi_params;  
    }  
  }  
}

静态文件缓存

http {  
  proxy_cache_path /path/to/cache levels=1:2 keys_zone=static_cache:10m max_size=1g inactive=24h;  
  server {  
    listen 80;  
    server_name example.com;  
  
    location ~* \.(html|css|js|jpg|jpeg|png|gif)$ {  
      root /path/to/web/root;  
      proxy_cache static_cache;  
      proxy_cache_valid 200 304 1w;  
      proxy_cache_key $host$uri$is_args$args;  
      expires 1w;  
    }  
  }  
}

NGINX高可用

nginx具备高可用特性,当一台nginx出现故障,可以迅速启用其他nginx服务器接管,继续稳定运行。

高可用方案:

nginx的高可用方案主要包括主从模式双主模式

主从模式:

一台服务器充当主服务器,另一台服务器作为备份服务器。当主服务器出现故障时,备份服务器将接管主服务器的任务,确保服务的连续性。

双主模式:

两台服务器互为主备,互相备份。这种模式在一定程度上提高了系统的可靠性,但也增加了复杂性。

  • 12
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值