【Nginx】Nginx Windows环境下实战演示

实战演示

Window下实战

windows下通过powershell启动jar包:

  • java -jar .\包名 --server.port=8080

  • java -jar .\包名 --server.port=8081

    在这里插入图片描述

浏览器访问8080和8081都可以访问成功;

但是这个是不合理的,所以在中间加一个Nginx代理,用户的请求到达Nginx,用户只需要访问一个80端口,由Nginx帮我们自动的转发请求到后台服务器上;

这就是反向代理,代理服务器端;

我们还希望8081端口的服务器上多一些请求,808端口的服务器上少一些请求,这也可以通过在Nginx中的配置进行实现;

所以需要做两个事情:

  1. 配置Nginx的监听端口为80端口,实现反向代理
  2. 配置8081和8080端口服务器的负载比重

配置操作:

  1. 打开 nginx - conf - nginx.conf

  2. 在80端口下做配置:

    • 配置反向代理 配置负载均衡

    在这里插入图片描述

  3. 重新加载nginx配置文件

    进入nginx.exe所在目录,cmd命令: nginx -s reload

  4. 通过powershall后端观察日志文件,改变权重,多次访问80端口,就可以观察到加权不同的情况下,nginx代理情况的负载比例;

实际项目中的配置:

  • 反向代理

  • 负载均衡

  • 动静分离

    html… 静态请求可以通过nginx处理,不用去请求tomcat

  • 重写

    80 http rewrite —> https 443

    就是说原来http 80端口的请求,我想让他走到 https 443端口

    在这里插入图片描述

    配置文件详解:

    http默认端口80;https默认端口443;

    # 1、全局配置
    # 全局配置中的所有配置都可以生效; 比如指定用户、进程信息、进程...
        #user  nobody;
        worker_processes  1;
    
        #error_log  logs/error.log;
        #error_log  logs/error.log  notice;
        #error_log  logs/error.log  info;
    
        #pid        logs/nginx.pid;
    
    # 2、events事件
    # 里面有最大连接数、监听的事件...
    events {
    	# 现在的事件是1024个
        worker_connections  1024;
    }
    
    # 3、http 
    # 静态资源文件基本的小配置都在这;里面有多个server{},就可以配置不同个服务
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
        #access_log  logs/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
        #gzip  on;
        
    	# 这里配置了80端口,80端口也是http的默认端口
        server {
            listen       80;
            server_name  localhost;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                root   html;
                index  index.html index.htm;
            }
    
            #error_page  404              /404.html;
    
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
    
            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
            #
            #location ~ \.php$ {
            #    proxy_pass   http://127.0.0.1;
            #}
    
            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
            #location ~ \.php$ {
            #    root           html;
            #    fastcgi_pass   127.0.0.1:9000;
            #    fastcgi_index  index.php;
            #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            #    include        fastcgi_params;
            #}
    
            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            #location ~ /\.ht {
            #    deny  all;
            #}
        }
    
    
        # another virtual host using mix of IP-, name-, and port-based configuration
        #
        #server {
        #    listen       8000;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    	# 现在有些网站会做https,所以监听443端口
        # HTTPS server
        #
        #server {
        #    listen       443 ssl;
        #    server_name  localhost;
    
        #    ssl_certificate      cert.pem;
        #    ssl_certificate_key  cert.key;
    
        #    ssl_session_cache    shared:SSL:1m;
        #    ssl_session_timeout  5m;
    
        #    ssl_ciphers  HIGH:!aNULL:!MD5;
        #    ssl_prefer_server_ciphers  on;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    }
    
    
    全局配置    
    
    events {
    	事件配置
        worker_connections  1024;
    }
    
    http {
        http全局配置
    	
    	upstream xx{
    		// 负载均衡配置  是为了给下面server中的location中的proxy代理配置负载
    		# 服务器资源 
    		server 127.0.0.1:8080 weight=1;
    		server 127.0.0.1:8081 weight=2;
    	}
    	
        server {
        	server配置
            listen       80;
            server_name  localhost;
    
            location /{
            	# 如果访问80端口的根目录,那么就走到这里请求配置   www.kuangstudy.com
            	# xxx   129.xxx  
            	
            	# 反向代理配置
            	proxy_pass http://kuangstudy
            }
            location /admin{
            	# xxx   47.xxx   如果访问根目录下的admin,就会走47这个服务器  www.kuangstudy.com/admin
            }
        }
        server {
        	server配置
            listen       443;
            server_name  localhost;
            // 代理配置
        }
    }
    
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宋大米Pro

感谢小主大赏,留言可进互助群~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值