Nginx基础概念功能学习笔记

编程入门 行业动态 更新时间:2024-10-11 03:23:06

Nginx简介

  • Nginx 是由俄罗斯人 Igor Sysoev 设计开发的,开发工作从2002 年开始,第一次公开发布在 2004 年 10 月 4 日。
  • Nginx 是 是一款免费开源的高性能 HTTP 代理服务器及反向代理服务器(Reverse Proxy)产品,同时它还可以提供 IMAP/POP3 邮件代理服务等功能。处理高并发能力是十分强大的,官方测试能够支撑 5 万的并发量;运行时内存和 CPU 占用率低,配置简单,容易上手,而且运行非常稳定。

Nginx可以干嘛?

正向代理:代理客户端

  • 正向代理:如果把局域网外的 Internet 想象成一个巨大的资源库,则局域网中的客户端要访 问 Internet,则需要通过代理服务器来访问,这种代理服务就称为正向代理。

反向代理:代理服务端

  • 反向代理,其实客户端对代理是无感知的,因为客户端不需要任何配置就可以访问。
    这是 Nginx 服务器作为 WEB 服务器的主要功能之一,客户端向服务器发送请求时,会首先经过 Nginx 服务器,由服务器将请求分发到相应的 WEB 服务器。正向代理是代理客户端,而反向代理则是代理服务器,Nginx 在提供反向代理服务方面,通过使用正则表达式进行相关配置,采取不同的转发策略,配置相当灵活,而且在配置后端转发请求时,完全不用关心网络环境如何,可以指定任意的IP地址和端口号,或其他类型的连接、请求等。

负载均衡

  • Nginx 最常用的功能之一,负载均衡,一方面是将单一的重负载分担到多个网络节点上做并行处理,每个节点处理结束后将结果汇总返回给用户,这样可以大幅度提高网络系统的处理能力;另一方面将大量的前端并发请求或数据流量分担到多个后端网络节点分别处理,这样可以有效减少前端用户等待相应的时间。而 Nginx 负载均衡都是属于后一方面,主要是对大量前端访问或流量进行分流,已保证前端用户访问效率,并可以减少后端服务器处理压力。

动静分离

  • 为了加快网站的解析速度,可以把动态页面和静态页面由不同的服务器来解析,加快解析速 度。降低原来单个服务器的压力。

Nginx的下载安装

下载地址

Windos安装教程

解压目录

查看conf目录下的nginx.conf文件

启动测试
我这里建议命令行执行nginx.exe,因为这里如果启动失败会有提示。如果启动失败检查本机的80端口是否被占用,我的就是被svn占用了。关闭即可。
然后启动访问

访问成功。
window下的相关命令

在nginx目录下打开cmd窗口

彻底关闭nginx
taskkill /f /t /im nginx.exe

启动nginx服务
start nginx


Linux安装教程


利用xftp把下载的文件上传到linux服务器

使用tar -zxvf nginx-1.20.2.tar.gz解压即可。

然后运行这个绿色的文件。
然后执行make install

下面进行启动测试:

[root@iZwz9hv1phm24s3jicy8x1Z nginx-1.20.2]# whereis nginx
nginx: /usr/local/nginx
[root@iZwz9hv1phm24s3jicy8x1Z nginx-1.20.2]# cd /
[root@iZwz9hv1phm24s3jicy8x1Z /]# ls
bin  boot  dev  etc  home  lib  lib64  lost+found  media  mnt  mydata  opt  patch  proc  root  run  sbin  srv  sys  tmp  usr  var  www
[root@iZwz9hv1phm24s3jicy8x1Z /]# cd /usr/local
[root@iZwz9hv1phm24s3jicy8x1Z local]# cd nginx
[root@iZwz9hv1phm24s3jicy8x1Z nginx]# ls
conf  html  logs  sbin
[root@iZwz9hv1phm24s3jicy8x1Z nginx]# cd ..
[root@iZwz9hv1phm24s3jicy8x1Z local]# ls
aegis  bin  btjdk  bttomcat  curl  etc  games  include  lib  lib64  libexec  libiconv  libsodium  man  nghttp2  nginx  openssl  rpcgen  sbin  share  src  zend
[root@iZwz9hv1phm24s3jicy8x1Z local]# cd nginx
[root@iZwz9hv1phm24s3jicy8x1Z nginx]# ls
conf  html  logs  sbin
[root@iZwz9hv1phm24s3jicy8x1Z nginx]# cd sbin
[root@iZwz9hv1phm24s3jicy8x1Z sbin]# ls
nginx
[root@iZwz9hv1phm24s3jicy8x1Z sbin]# ./nginx
[root@iZwz9hv1phm24s3jicy8x1Z sbin]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx/">nginx</a>.<br/>
Commercial support is available at
<a href="http://nginx/">nginx</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

下面通过外网访问测试,一样可以访问成功。
(相信学习到这里的小伙伴一定知道安全组要如何打开了,所以这里不再赘述可能访问不到的情况,直接访问即可)

Nginx的相关命令

[root@iZwz9hv1phm24s3jicy8x1Z ~]# whereis nginx
nginx: /usr/local/nginx
[root@iZwz9hv1phm24s3jicy8x1Z ~]# cd /usr/local/nginx
[root@iZwz9hv1phm24s3jicy8x1Z nginx]# ls
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
[root@iZwz9hv1phm24s3jicy8x1Z nginx]# cd sbin
[root@iZwz9hv1phm24s3jicy8x1Z sbin]# ls
nginx

关闭nginx服务
[root@iZwz9hv1phm24s3jicy8x1Z sbin]# ./nginx -s stop
启动nginx服务
[root@iZwz9hv1phm24s3jicy8x1Z sbin]# ./nginx
安全退出
[root@iZwz9hv1phm24s3jicy8x1Z sbin]# ./nginx -s quit
重新加载配置文件(运行的时候修改配置文件之后要重新加载才会生效)
[root@iZwz9hv1phm24s3jicy8x1Z sbin]# ./nginx -s reload

有关linux的防火墙的相关命令

# 开启
service firewalld start
# 重启
service firewalld restart
# 关闭
service firewalld stop
# 查看防火墙规则
firewall-cmd --list-all
# 查询端口是否开放
firewall-cmd --query-port=8080/tcp
# 开放80端口
firewall-cmd --permanent --add-port=80/tcp
# 移除端口
firewall-cmd --permanent --remove-port=8080/tcp

#重启防火墙(修改配置后要重启防火墙)
firewall-cmd --reload

# 参数解释
1、firwall-cmd:是Linux提供的操作firewall的一个工具;
2、--permanent:表示设置为持久;
3、--add-port:标识添加的端口;

Nginx配置文件详解

第一部分:从配置文件开始到 events 块之间的内容,主要会设置一些影响nginx 服务器整体运行的配置指令,主要包括配 置运行 Nginx 服务器的用户(组)、允许生成的 worker process 数,进程 PID 存放路径、日志存放路径和类型以 及配置文件的引入等。

#user  nobody;
worker_processes  1;#这是 Nginx 服务器并发处理服务的关键配置,worker_processes 值越大,
#可以支持的并发处理量也越多,但是 会受到硬件、软件等设备的制约。

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

第二部分:events块
主要影响 Nginx 服务器与用户的网络连接,常用的设置包括是否开启对多 work process 下的网络连接进行序列化,是否 允许同时接收多个网络连接,选取哪种事件驱动模型来处理连接请求,每个 word process 可以同时支持的最大连接数等。

events {
    worker_connections  1024;
}

第三部分:这算是 Nginx 服务器配置中最频繁的部分,代理、缓存和日志定义等绝大多数功能和第三方模块的配置都在这里。
详细介绍
第三部分又分为两个部分http块server全局块

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  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            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;
        }

        # 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;
    #    }
    #}

}


更多推荐

Nginx基础概念功能学习笔记

本文发布于:2023-06-13 16:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1385394.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:学习笔记   概念   功能   基础   Nginx

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!