一、nginx是什么?(简单介绍)

1.nginx就是反向代理服务器。代理服务器是介于客户端和Web服务器之间的另一台服务器,有了它之后,浏览器不是直接到Web服务器去取回网页,而是通过向代理服务器发送请求,信号会先送到代理服务器,由代理服务器来取回浏览器所需要的信息并传送给你的浏览器。这样我们就可以用它来实现跨域的操作了。如下图:

nginx配置springboot后端代理 spring nginx_代理服务器

二、正式开始

第一步:没有nginx的话要先安装(我这里不详细介绍),给大家推荐一篇文章:windows下安装nginx

第二步:安装好nginx过后,重要的就是配置nginx.conf文件了,如下图:

nginx配置springboot后端代理 spring nginx_nginx_02


打开它,有好多配置,你不要管,全删了,把我的拷进去(我会解释这些配置是什么意思)

worker_processes  1;

events {
    worker_connections  1024;
}

http { 
       server {
            listen       801;
            server_name  localhost;

        location / {
            root  html;
            index  index.html index.htm; 
        }
        location /api/ {  #api这个名字是需要在页面url配置的。                
              proxy_pass http://localhost:8080/;  #注意:使用代理地址时末尾记得加上斜杠"/"。
                
        }
    }      
}

nginx.conf重要模块介绍:
1、events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
2、http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
3、server块:配置虚拟主机的相关参数,一个http中可以有多个server。
4、location块:配置请求的路由,以及各种页面的处理情况。

nginx.conf单个字段介绍:
worker_processes 1:允许生成的进程数,默认为1
worker_connections 1024:最大连接数,默认为512
listen 801:监听端口
server_name localhost: 监听地址
root path: 根目录
index index.html index.htm : 设置默认页
注意:我这里只是最最最基础的配置了,想详细了解的话,还是花很多时间学习的!

第三步: nginx.conf基本配置好后,找到如下位置(就是你安装nginx的位置),在html创建一个html页面,我这里是user.html,你随意(记得找个js导入),如下图:

nginx配置springboot后端代理 spring nginx_html_03


user.html页面内容如下(可以直接copy):

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <title>nginx测试</title>
  <style>
    body{margin: 10px;}
    .demo-carousel{height: 200px; line-height: 200px; text-align: center;}
  </style>
  <script src="/js/jquery.min.js"></script>
</head>
<body>
 
<button id="btn" onclick="btn()">点我</button>
<h3 id="aaa"></h3>

<script>
    function btn(){
        alert("请求了!!!");
        //向服务端发送删除指令
		 $.ajax({
            url: 'api/selectAll',
            type: "POST",
            success: function (res) {
               alert(res);    
        //取json中的name,打印在页面中             
       document.getElementById("aaa").innerHTML=res; 
            },
            error: function (res) {
			   alert(res)
               alert("跨域失败!");
            }
        });
    }
</script>
</body>
</html>

第四步: 到目前,nginx这一块差不多搞完了,现在就是建一个springboot项目了(这个就不多说了,相信大家都会!),然后写一个controller类,如下:

@Controller
@RestController
public class userController {

    @RequestMapping("/selectAll")
    public String selectAll(HttpServletResponse response){
        //允许所有域名访问
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");
        return "Hello,World!";
    }

你不改端口默认就是8080,那个上面的nginx.conf配置文件,我的代理地址也是8080,不用改,默认就好了!后面启动这个项目就好了!

第五步:启动你的nginx,直接双击那个nginx.exe(那个绿色的就好了),一闪而过就说明启动成功了,你也可以用命令启动,首先进入到nginx安装目录,打开cmd,输入 start nginx就可以了!

所有的准备工作都做好了,现在就是测试了,首先访问http://localhost:801/user.html,能看到有个按钮的页面就表示成功了,然后点击那个按钮,有“Hello,World”返回就说明成功了!

nginx配置springboot后端代理 spring nginx_代理服务器_04