NGINX

nginx

安装

这里使用了乌邦图的linux下载nginx

Nginx http或者https代理,反向代理:作为web服务器最常见功能之一

正向代理:使用VPN,让VPN服务器代理访问目标服务器,将获得的内容返回到主机

Linux

  1. sudo apt update 这里的update是为了更新包的列表
  2. sudo apt install nginx

Window

scoop install nginx

choco install nginx

使用

报错

用户权限不足,使用root登录

报错文

nginx: [alert] could not open error log file: open() “/var/log/nginx/error.log” failed (13: Permission denied)

2024/02/25 11:41:30 [warn] 3324#3324: the “user” directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:1

2024/02/25 11:41:30 [emerg] 3324#3324: open() “/var/log/nginx/access.log” failed (13: Permission denied)

解决方法

使用su登录

端口被占用

报错文

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Unknown error)
nginx: [emerg] bind() to [::]:80 failed (98: Unknown error)

nginx: [emerg] still could not bind()

解决方法

可能是因为没有使用su登录,直接开启nginx,nginx报错,但是对应端口已打开,关闭nginx,使用su重新打开

Nginx的命令

在Linux Shell中输入nginx,即可启动,如果信息弹出,那就是启动成功,如果有报错,百度一下就好了

在Shell中输入nginx -s 加上后缀

quit stop reload reopen
正常关闭 立即关闭 重新配置文件 重新打开日志文件

ps aux | grep nginx 查看nginx的进程

查看Nginx的开启服务 查看对应端口的使用情况

在Shell中 ps -ef | gref nginx

在Shell中 lsof -i:80 查看80端口占用情况

Nginx的信息查看 配置文件路径

在Shell中输入 nginx -v 只查看版本信息

在Shell中输入 nginx -V 查看所有信息,在信息中找到 –conf-path= 后面的内容,找到配置文件

或者使用nginx -t对配置文件进行验证,并且有路径的输出

Nginx的负载均衡

Nginx的负载均衡有两种 内置策略和扩展策略

内置策略

轮询,加权轮询,Ip Hash

轮询 设置两个代理服务器,假如1号服务器和2号服务器的性能相近,那么他们之间可以进行轮询

加权轮询 如果1号服务器比2号服务器的性能好,那么可以对其进行加权,让1号服务器的加权高于2号服务器的加权,可以让大部分的访问到1号服务器上

扩展策略

后续补一下

Nginx的nginx.conf

# http协议下的配置
# 监听 8088 端口,将接到8088端口转到 8001端口上,完成反向代理
http {
    server {
        listen       8088;
        server_name localhost;
        location /{
            proxy_pass http://localhost:8001;
        }
    }
}


NGINX
http://example.com/2024/02/28/nginx/
作者
smg
发布于
2024年2月28日
许可协议