• Nginx 简单介绍(一)


    1. nginx
        1. 代理
            1. nginx 更像 一个壳,包裹了内网服务和外界的交互,让内网的服务更专注于业务的处理本身。
            2. 所谓的代理,是代替内网服务处理和外界的交互。
            3. 原本的单进程模式或者cs bs 直接处理的方式,将业务和链接一起处理,耦合性更高。
            4. 使用nginx的方式,可以降低耦合性。也方便后台业务的维护,因为:可以实现链接和处理的分离,也就降低了更新维护的成本。
        2. 配置代理
            1. nginx采用配置的方式,实现它的代理功能,
            2. 配置在 conf 目录下的 `*.conf` 文件中。
            3. 配置核心是:`http` `server`  等关键字。
            4. 配置可使用关键字 可在开源代码中找到,它使用Key 的方式进行检索处理
                ```conf
                http {
                    server {
                    }
                }
                ```
            5. 我们先不考虑nginx的源码实现,这里先了解他的用法:他采用web的域名访问方式 进行分割和处理。 如果我们想要让前端的访问请求直接转到 指定地址或者指定服务器上的指定端口可以这样做:
                ```conf
                http {
                 server {
                     listen 8000;
                     location / {
                         root /usr/local/nginx/html/www;
                     }
                 }
                    server {
                        listen 9000;
                        location / {
                            proxy_pass http://192.168.1.10:8888;
                        }
                     }
                }
                ```
            6. 如果我们想要将数据转接到多个服务器可以这样写: [^1]
                ```conf
                http {
                    upstream test{
                        server 192.186.1.10;
                        server 10.16.1.101:8888;
                    }
                    server {
                         listen 9000;
                         location / {
                            proxy_pass http://test;
                         }
                     }
                }
                ```
            7. 如果想引入负载均衡可以这样写:`weight=9 ` 这里的语义是配置权重
                ```conf
                http {
                    upstream test{
                        server 192.186.1.10    weight=9;
                        server 10.16.1.101:8888  weight=1;
                    }
                    server {
                         listen 9000;
                         location / {
                            proxy_pass http://test;
                         }
                     }
                }
                ```
            8. 如果内容过多,可以将多个server 分开写到不同的`.conf`文件中。在主配置文件中使用 `include`加载 例如:
                ```conf
                http {
                    upstream test{
                        server 192.186.1.10    weight=9;
                        server 10.16.1.101:8888  weight=1;
                    }
                    include ./conf_SVR_1/Svr.conf;
                    include ./conf_SVR_2/Svr.conf;
                    include ./conf_SVR_3/Svr.conf;
                    include ./conf_SVR_4/Svr.conf;
                    include ./conf_SVR_5/Svr.conf;
                    include ./conf_SVR_6/Svr.conf;
                    include ./conf_SVR_Type/*.conf;
                }
                ```
                在各个副配置文件中就只需要写`serve`块中内容。不要再写 `http`了。

        3. 安装命令
            ```shell
            ./configure --prefix=/usr/local/nginx --with-http_realip_module --with-http_addition_module --with-http_ssl_module --with-http_gzip_static_module --with-http_secure_link_module --with-http_stub_status_module --with-stream --with-pcre=/home/king/share/nginx/pcre-8.45 --with-zlib=/home/king/share/nginx/zlib-1.2.13 --with-openssl=/home/king/share/nginx/openssl-1.1.1s
            ```
        4. 使用nginx 编译需要加载的库, 假设你需要对Nginx二次开发,这里以`ngx_code.c`为例子:
            ```shell
            gcc -o ngx_code ngx_code.c -I ./nginx-1.22.1/src/core/ -I ./nginx-1.22.1/objs/ -I ./nginx-1.22.1/src/os/unix/ -I ./pcre-8.45/ -I ./nginx-1.22.1/src/event/ -I ./openssl-1.1.1s/include/ -I ./nginx-1.22.1/src/event/modules/
            ```
        5. 补充内容 :《Nginx反向代理与系统参数配置conf原理》[^2]

    [^1]:如果只有IP 没有端口,说明是默认的端口 80 。

    [^2]:![[Nginx反向代理与系统参数配置conf原理.pdf]]

  • 相关阅读:
    Codeforces Round #790 (Div. 4) E. Eating Queries
    【Maven】基础快速入门
    GBase 8d的技术指标
    selenium——chromdriver版本请及时更新
    1.5-07:奥运奖牌计数
    第2章 进程管理
    sqlite数据库乱码
    【wireshark】如何获取一个设备的IP地址
    Vue3中的pinia使用,入门教程
    骑行运动耳机好不好用,推荐五款最适合骑行的运动耳机
  • 原文地址:https://blog.csdn.net/qq_29111047/article/details/127876834