nginx的动静分离

  使用nginx的动静分离可以让多个不同的Web服务器各司其职,涉及Servlet、Jsp页面处理交给Tomcat处理,涉及PHP页面处理交给apache,asp.net页面交个IIS,py的页面交给py的web服务器处理,而静态页面都交给nginx处理。

  servlet的处理可以加后缀,比如后缀为.do,这样标识方便nginx处理。

  修改conf下的nginx.conf配置:

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;

    server {
        listen       9001;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   G:/nginx/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;
        }

        # PHP===》PHP文件的处理
        location ~ \.php$ {
            proxy_pass   http://127.0.0.1:80;
        }

        #Java===》servlet和JSP页面的处理
        location ~ \.(jsp|do)$ {
            proxy_pass   http://127.0.0.1:9019;
        }
    }
}

  启动Apache、Tomcat、nginx,其中Apache运行于80端口,Tomcat运行于9019端口,nginx运行于9001端口。

  编写测试页面:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>获取Java数据</title>
		<script src="http://127.0.0.1:9001/js/jquery-3.4.1.min.js"></script>
	</head>
		
	<body>
		<button onclick="GetData1()">获取Java-Servlet数据</button>
		<div id="demo1"></div>
		<hr>
		<button onclick="GetData2()">获取PHP数据</button>
		<div id="demo2"></div>
		<script>
			function GetData1(){
				$.ajax({
						url: 'http://127.0.0.1:9001/JavaWeb2022/servletdemo.do', //请求的url地址
						dataType: "text", 
						async: true, //请求是否异步,默认true异步,这是ajax的特性
						data:{
							name:"QWE",
							age:13
						},
						type: "POST", //请求的方式
						beforeSend:function(){}, //请求前的处理
						success: function(data) { // 请求签的处理
								console.log(data);
								document.getElementById("demo1").innerHTML = document.getElementById("demo1").innerHTML + data+"<br>";
						}
				 });				
			}

			function GetData2(){
				$.ajax({
						url: 'http://127.0.0.1:9001/PHP2021/test/test.php', //请求的url地址
						dataType: "text", 
						async: true, //请求是否异步,默认true异步,这是ajax的特性
						data:{
							name:"QWE",
							age:13
						},
						type: "POST", //请求的方式
						beforeSend:function(){}, //请求前的处理
						success: function(data) { // 请求签的处理
								console.log(data);
								document.getElementById("demo2").innerHTML = document.getElementById("demo2").innerHTML + data+"<br>";
						}
				 });				
			}
			
		</script>
	</body>
</html>

  运行:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值