Nginx开发01:基础配置

发布于:2025-02-10 ⋅ 阅读:(27) ⋅ 点赞:(0)

一、下载和启动

1.下载、使用命令行启动:Web开发:web服务器-Nginx的基础介绍(含AI文稿)_nginx作为web服务器,可以承担哪些基本任务-CSDN博客

注意:我配置的端口是81

2.测试连接是否正常

访问Welcome to nginx! 如果没有报错,出现下面的界面则表示Nginx配置正常!

 二、常用命令

1.启动命令

start nginx

2.退出命令

选择1:安全退出

nginx -s quit

选择2:强制退出(stop 参数会立即停止 Nginx 进程,而 quit 参数会等待当前请求处理完成后再停止)

nginx -s stop

选择3:强制杀掉线程:查询进程号和终止其所有进程(管理员身份运行cmd)

tasklist | findstr "nginx.exe"

taskkill /f /t /im nginx.exe

 3.重新加载配置文件(使新的配置生效)

nginx -s reload

三、常见玩法

1.映射访问静态资源

第一步,建立文件:

E:\Test\nginx-logo.png

第二步,Nginx配置映射

listen       81;
server_name  localhost;

location /PicTest
{
    alias E:/Test;
}

说直白点,就是访问/PicTest相当于访问E:/Test 

 (保存了conf配置文件之后,需要去执行一下reload命令才保存成功!)

第三步,用浏览器访问以下两个url都可以正常访问到图片,说明映射成功!

E:/Test/nginx-logo.png

http://localhost:81/PicTest/nginx-logo.png

 ​​​​​​

附上完整配置文件(nginx.conf):

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;


    server {
        listen       81;
        server_name  localhost;

		location /PicTest
		{
			alias E:/Test;
		}
		
		
        location / {
            root   html;
            index  index.html index.htm;
        }


        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }
}

2.负载均衡转发接口

第一步,确保下面两个接口能正常收到数据

http://localhost:5050/api/getData

http://localhost:5055/api/getData

第二步,配置Nginx

效果是:当访问 http://localhost:81/api/getData 时,都有概率打到上面的两个接口中(权重=5055:5050=2:1

http {
    upstream backend {
        # 定义负载均衡的后端服务器,并设置权重
        server localhost:5050 weight=1;  # 5050 端口的服务器权重 1
        server localhost:5055 weight=2;  # 5055 端口的服务器权重 2
    }

    server {
        listen 81;  # 监听 81 端口

        location /api/getData {
            # 转发请求到 upstream 定义的服务器集群
            proxy_pass http://backend;  # 使用上面定义的 upstream 名称
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
}

 (保存了conf配置文件之后,需要去执行一下reload命令才保存成功!) 

第三步,访问Nginx端口

http://localhost:81/api/getData

访问上面接口时,能正常返回数据,并且发现每请求4次5055后会请求2次5050(权重2:1),并且循环如此,说明均衡负载配置成功!

附上完整配置文件(nginx.conf):

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    upstream backend {
        # 定义负载均衡的后端服务器,并设置权重
        server localhost:5050 weight=1;  # 5050 端口的服务器权重 1
        server localhost:5055 weight=2;  # 5055 端口的服务器权重 2
    }
	
    server {
        listen       81;
        server_name  localhost;

		location /api/getData {
            # 转发请求到 upstream 定义的服务器集群
            proxy_pass http://backend;  # 使用上面定义的 upstream 名称
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
		
        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}


网站公告

今日签到

点亮在社区的每一天
去签到