nginx的配置问题

it2024-07-11  49

总体配置范例:

worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream group1{ #负载均衡组 #这里可以设置负载均衡策略,默认为轮询,指定权重后为加权轮询 server 127.0.0.1:8091 max_fails=3 fail_timeout=30s weight=20; #尝试失败次数为3,失败超时时间为30秒,权重为20 server 127.0.0.1:8091 max_fails=3 fail_timeout=30s weight=50; server 127.0.0.1:8085 backup; #备用机,在非备用机都宕机后启用 } server { listen 80; #访问端口 server_name www.hehe.com; #访问域名,配合hosts文件下设定的本机ip,或者在本机hosts找不到对应ip后,直接去网络上搜索对应域名ip映射 location / { root html; index index.html index.htm; } location /c { root /usr/local/Cellar/nginx/1.17.6/test1/; #静态资源访问,root的作用相当于在访问"www.hehe.com:80/c/"时会访问到"/usr/local/Cellar/nginx/1.17.6/test1/c"下的内容,c后加"/"则输入url后必须有"/" } location /b { alias /usr/local/Cellar/nginx/1.17.6/test1/; #静态资源访问,alias的作用相当于在访问"www.hehe.com:80/b/"时会访问到"/usr/local/Cellar/nginx/1.17.6/test1"下的内容,b后加"/"则输入url后必须有"/" } location /a { proxy_pass http://group1/; #反向代理,a后加不加"/"不影响 } error_page 502 https:www.bilibili.com; #502错误后调用的服务 error_page 404 https:www.baidu.com; #404错误后调用的服务 } }

hosts增加一行配置:

127.0.0.1 www.hehe.com

用于确立本机地址和域名"www.hehe.com"的映射关系。

1、静态资源加载 注意路径配置问题中的root和alias: root会指向root+location合并后的资源路径。 alias会直接指向alias定义的资源路径。

2、反向代理 由proxy_pass定义

3、负载均衡 由upstream定义组来确立

最新回复(0)