nginx 快速入门学习

静态网页服务器

Nginx 是一个 http 服务可以独立提供 http 服务。可以做网页静态服务器。
进入到conf目录下

cd conf

打开配置文件nginx.conf

vi nginx.conf
server {
    
    
        listen       80; # 监听的端口
        server_name  localhost; # 域名或ip
        location / {
    
    	# 访问路径配置
            root   html;# 根目录
            index  index.html index.htm; # 默认首页
        }
        error_page   500 502 503 504  /50x.html;	# 错误页面
        location = /50x.html {
    
    
            root   html;
        }
    }    

默认访问的是html目录下的index.html、index.htm
这里可以通过配置不同的端口来实现访问不同的目录下的静态页面

server {
    
    
        listen       81; # 监听的端口
        server_name  localhost; # 域名或ip
        location / {
    
    	# 访问路径配置
            root   hello1;# 根目录
            index  hello1.html hello1.htm; # 默认首页
        }
        error_page   500 502 503 504  /50x.html;	# 错误页面
        location = /50x.html {
    
    
            root   html;
        }
    }


     server {
    
    
        listen       82; # 监听的端口
        server_name  localhost; # 域名或ip
        location / {
    
    	# 访问路径配置
            root   hello2;# 根目录
            index  hello2.html; # 默认首页
        }
        error_page   500 502 503 504  /50x.html;	# 错误页面
        location = /50x.html {
    
    
            root   html;
        }

        
    }

地址栏输入http://192.168.182.129/:81 可以看到hello1页面
地址栏输入http://192.168.182.129/:82 可以看到hello2页面

反向代理与负载均衡

反向代理就是通过nginx使作为代理服务器来转发给内部服务器
在这里插入图片描述

负载均衡就是根据服务器的性能来配置不同的权重,能力越大责任越大。

upstream hello {
    
    
	   server 192.168.182.129:8080 weight=2;
	   server 192.168.182.130:8080;
	   
    }

    server {
    
    
        listen       80; # 监听的端口
        server_name  localhost; # 域名或ip
        location / {
    
    	# 访问路径配置
            # root   index;# 根目录
	    proxy_pass http://hello;

            index  index.html index.htm; # 默认首页
        }
        error_page   500 502 503 504  /50x.html;	# 错误页面
        location = /50x.html {
    
    
            root   html;
        }
    }

帮助到您请点赞关注收藏谢谢!!

猜你喜欢

转载自blog.csdn.net/weixin_43423864/article/details/109088819
今日推荐