nginx 【一 配置和使用】

一、下载部署

环境:windows 10 专业版、nginx 1.25.1 ,nginx下载地址:http://nginx.org/,下载后解压。

二、运行

进入解压根目录,运行命令:

nginx -c conf/nginx.conf

访问localhost出现 “Welcome to nginx!” 标识成功启动。

常用命令:

nginx -?,-h 打开帮助信息

nginx -v 显示版本信息并退出

nginx -V 显示版本和配置选项信息,然后退出

nginx -c filename 启动时设置配置文件(linux默认是:/etc/nginx/nginx.conf,windows是解压目录的conf目录下)

nginx -s reopen 重启Nginx

nginx -s reload 重新加载Nginx配置文件,然后以优雅的方式重启Nginx

nginx -s stop 强制停止Nginx服务

nginx -s quit 优雅地停止Nginx服务(即处理完所有请求后再停止服务)

nginx -p prefix 设置前缀路径(默认是:/usr/share/nginx/)

nginx -g directives 设置配置文件外的全局指令

nginx -t 检测配置文件是否有语法错误,然后退出,不会真正启动服务

nginx -T 检测配置文件是否有语法错误,转储并退出

nginx -q 在检测配置文件期间屏蔽非错误信息

三、http 配置

http 模块是nginx 进行 http协议代理转发的。配置示例如下:

http {
    # http 模块通用配置
    include       mime.types;
    default_type  application/octet-stream;
    keepalive_timeout  65;
    gzip  on;
    # 后端主机列表配置
    upstream upserver {
        server ip1:port1;
        server ip2:port2;
        server ip3:port3;
    }
    #服务配置,服务名:server.com
    server {
        listen 80;
        server_name server.com;
        location / {
            # 负载均衡代理
            proxy_pass http://upserver;
        }
    }
}
更改完配置,执行 “nginx -s reload” 命令,然后访问:http://server.com。(注意:server.com 是在本机环境下,分布式环境下 server.com 需要进行域名映射)

四、stream 配置

stream模块是nginx 进行 tcp/udp 协议代理转发的。配置示例如下:


# stream配置,和http是平级模块,此实例配置代理数据
stream {
    #后端数据实例配置
    upstream mysql{
        server ip1:3306;
        server ip2:3306;
    }
    # 代理服务配置
    server {
        listen 8899;
        proxy_connect_timeout 5s;  # 与被代理服务器建立连接的超时时间为5s
        proxy_timeout 10s;   # 获取被代理服务器的响应最大超时时间为10s
        proxy_next_upstream on;  # 当被代理的服务器返回错误或超时时,将未返回响应的客户端连接请求传递给upstream中的下一个服务器
        proxy_next_upstream_tries 3;   # 转发尝试请求最多3次
        proxy_next_upstream_timeout 10s;    # 总尝试超时时间为10s
        proxy_socket_keepalive on;  # 开启SO_KEEPALIVE选项进行心跳检测
        #代理负载均衡
        proxy_pass mysql;
    }
}

更改完配置,执行 “nginx -s reload” 命令,然后通过数据库工具连接8899端口,即可通过代理以负载均衡的方式访问后端数据库。



posted @ 2023-08-01 17:13  蓝迷梦  阅读(204)  评论(0)    收藏  举报