Nginx 简单介绍(一)

简介: 如果我们想要将数据转接到多个服务器可以这样写: [^1]

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]]

目录
相关文章
|
23天前
|
缓存 负载均衡 Java
什么是Nginx服务?
Nginx是一个高性能的开源的HTTP和反向代理服务器,以及邮件(IMAP/POP3)代理服务器。它最初由Igor Sysoev创建,并于2004年首次公开发布。Nginx的主要特点包括高性能、低内存占用、高并发处理能力以及高度的可靠性。
25 2
|
2月前
|
缓存 负载均衡 应用服务中间件
nginx 就该这么用
nginx 就该这么用
20 0
|
4月前
|
负载均衡 前端开发 应用服务中间件
|
10月前
|
负载均衡 前端开发 安全
nginx能帮我们做什么?
Nginx是一款高性能的开源Web服务器软件,它可以帮助我们完成以下几个方面的任务:
68 0
|
9月前
|
负载均衡 应用服务中间件 nginx
nginx V1.0
nginx V1.0
|
XML 缓存 应用服务中间件
|
应用服务中间件 nginx
Nginx - autoindex
Nginx - autoindex
272 0
Nginx - autoindex
|
Web App开发 负载均衡 前端开发
nginx使用分享
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like协议下发行。Nginx的并发能力在同类型的Web服务器中表现优异,国内主流无论大小互联网公司都在用。
571 0
nginx使用分享
|
应用服务中间件 开发工具 nginx
Nginx之15独孤九剑 - (GoogleFilter)
Nginx从入门到深入之Google Proxy
1036 0
|
Unix 应用服务中间件 PHP