windows nginx 初体验

下载安装

链接 http://nginx.org/en/download.html

解压运行 进入相应的目录下,执行 start nginx 启动;nginx.exe -s reload 重启;(修改nginx需要重启)


nginx.conf


#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;


events {
    worker_connections  1024;
}


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       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location ^~ /api/login {
          proxy_pass   http://127.0.0.1:8899;
        }
		
		location ^~ /api {
          proxy_pass   http://cymanservice;
        }
		
		location ^~ /test {
          proxy_pass   http://cymantest;
        }
		
		
		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;
        }

    }

	upstream cymanservice {
	  #餐饮后端管理系统
	  server 127.0.0.1:8899 weight=1 max_fails=2 fail_timeout=30;
	}
	
	upstream cymantest {
	  #餐饮后端管理系统
	  server 127.0.0.1:8899 weight=1 max_fails=2 fail_timeout=30;
	}
	

}


index.html


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p><em>Thank you for using nginx.</em></p>
<h1>当你看到这句话的时候,说明我已经初步掌握了nginx</h1>
</body>


<!-- 引入jq -->
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
    getNginx()
	getNginx1()
	getNginx2()
    function getNginx() {
        let data = {
            "passWord": "123456",
            "userAccount": "123456"
        }
        $.ajax({
            url: "/api/login", //请求的url地址  相当于http://127.0.0.1:8899/api/login
            dataType: "json", //返回格式为json
            async: true, //请求是否异步,默认为异步,这也是ajax重要特性
            contentType: "application/json", //传递的格式为json格式
            data:JSON.stringify(data), //参数值
            type: "POST", //请求方式
            beforeSend: function () {
                //请求前的处理
            },
            success: function (req) {
                //请求成功时处理
                console.log(req)
            },
            complete: function () {
                //请求完成的处理
            },
            error: function () {
                //请求出错处理
            }
        });
    }
	
	
	
    function getNginx1() {
        let data = {
            "passWord": "123456",
            "userAccount": "123456"
        }
        $.ajax({
            url: "/api/logout", //请求的url地址  相当于http://127.0.0.1:8899/api/logout
            dataType: "json", //返回格式为json
            async: true, //请求是否异步,默认为异步,这也是ajax重要特性
            contentType: "application/json", //传递的格式为json格式
            data:JSON.stringify(data), //参数值
            type: "POST", //请求方式
            beforeSend: function () {
                //请求前的处理
            },
            success: function (req) {
                //请求成功时处理
                console.log(req)
            },
            complete: function () {
                //请求完成的处理
            },
            error: function () {
                //请求出错处理
            }
        });
    }
	
	function getNginx2() {
        let data = {
            "passWord": "123456",
            "userAccount": "123456"
        }
        $.ajax({
            url: "/test/test111", //请求的url地址  相当于http://127.0.0.1:8899/test/test111
            dataType: "json", //返回格式为json
            async: true, //请求是否异步,默认为异步,这也是ajax重要特性
            contentType: "application/json", //传递的格式为json格式
            data:JSON.stringify(data), //参数值
            type: "POST", //请求方式
            beforeSend: function () {
                //请求前的处理
            },
            success: function (req) {
                //请求成功时处理
                console.log(req)
            },
            complete: function () {
                //请求完成的处理
            },
            error: function () {
                //请求出错处理
            }
        });
    }
</script>
</html>

后台应用

IndexController

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author :lichuankang
 * @date :Created in 2022/2/9 15:25
 * @description :
 */
@RestController
@RequestMapping(value = "/api")
public class IndexController {

    @RequestMapping(value = "/login")
    public String login() {
        System.out.println("IndexController login   is run");
        return "ok!!!";
    }

    @RequestMapping(value = "/logout")
    public String logout() {
        System.out.println("IndexController logout   is run");
        return "ok!!!";
    }
}

TestController

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author :lichuankang
 * @date :Created in 2022/2/9 15:46
 * @description :
 */
@RestController
@RequestMapping(value = "/test")
public class TestController {

    @RequestMapping(value = "/test111")
    public String test111() {
        System.out.println("TestController test111   is run");
        return "ok!!!!";
    }

    @RequestMapping(value = "/test222")
    public String test222() {
        System.out.println("TestController test222   is run");
        return "ok!!!!";
    }
}

application.properties

server.port=8899

浏览器访问: http://localhost/

posted @ 2022-02-09 16:04  lick  阅读(57)  评论(0编辑  收藏  举报