锋盈数科-知识库 Logo
首页
软件开发
计算机基础
Hello Halo
新手必读
关于本知识库
登录 →
锋盈数科-知识库 Logo
首页 软件开发 计算机基础 Hello Halo 新手必读 关于本知识库
登录
  1. 首页
  2. 软件开发
  3. JAVA
  4. Nginx(http配置、https配置)访问Spring Boot 项目

Nginx(http配置、https配置)访问Spring Boot 项目

0
  • JAVA
  • 发布于 2024-08-16
  • 0 次阅读
黄健
黄健

本文由 简悦 SimpRead 转码, 原文地址 blog.csdn.net

前文

记录一下在 linux 服务器下配置 nginx 中 nginx.conf 文件代理访问 springboot 项目

  1. spring boot.yml 配置

其他 mysql,redis,mybatis 等之类的配置就不一一列出了

# 自定义配置 为了等下验证读取的配置文件环境
appName: product

server:
  port: 8083  # 应用服务 WEB 访问端口
  servlet:
    context-path: /api
    session:
      timeout: PT60M #session过期时间 60M 一个小时
  1. nginx 配置

查看 nginx 安装目录

whereis nginx

修改 nginx.conf 即可, 一般配置文件都在 /usr/local/nginx/conf 目录下

2.1 https 配置

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

    #将服务所有的 http 请求转换为 https 请求
    server {
    	listen    80;
      # listen       8081; 
      # listen       443 ssl;
      listen        442 ssl;
      # 因为服务器没有完成备案导致我的80,443端口不能开放,所以我这里演示使用8081,442
      # 我的nginx配置文件上方的80端口是注释掉的,
      #你的域名,请不要带有http://或者https://
    	server_name  xxx.com;
      #charset koi8-r;

      #access_log  logs/host.access.log  main;
    
      location / {
            #网站主页路径。此路径仅供参考,具体请您按照实际目录操作。  
            root html;
            index index.html index.htm;
        }
 
      #此处呼应 spring boot 应用内的 servlet.context-path 配置
      #说明: 如果你访问 www.xxxx.com/api 将会请求转发到服务器内的 127.0.0.1:8083 服务
      location /api {
      	    proxy_pass http://127.0.0.1:8083;
      	    proxy_set_header           Host $host;
            proxy_set_header  X-Real-IP  $remote_addr;
            proxy_set_header           X-Forwarded-For       $proxy_add_x_forwarded_for;
            client_max_body_size  100m;
      }
    
      #说明: 这里模拟静态资源读取,示例请求url: http://www.xxxx.com/app_images/xxx.png
      #访问 www.xxxx.cn/app_images/xxx.png 会转发访问服务器内的绝对路径/usr/local/app_images/xxx.png
      location /app_images {
    	    root /usr/local;
      }       
    
      # 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;
      }
    
    }

    
}

2.2 http 配置

上传证书到 conf 目录下, 创建一个 cert 文件夹方便管理

免费证书申请可以使用阿里云, 腾讯云, https://freessl.cn / 等

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

    #将服务所有的 http 请求转换为 https 请求
    server {
    	listen    80;
      # 因为服务器没有完成备案导致我的80端口不能开放,所以我这里演示使用下面的8081
      # 我的nginx配置文件上方的80端口是注释掉的,
      # listen       8081; 
      #你的域名,请不要带有http://或者https://
    	server_name  xxxx.com;
      #charset koi8-r;

      #access_log  logs/host.access.log  main;
    
      location / {
            #网站主页路径。此路径仅供参考,具体请您按照实际目录操作。  
            root html;
            index index.html index.htm;
        }
 
      #此处呼应 spring boot 应用内的 servlet.context-path 配置
      #说明: 如果你访问 www.xxxx.com/api 将会请求转发到服务器内的 127.0.0.1:8083 服务
      location /api {
      	    proxy_pass http://127.0.0.1:8083;
      	    proxy_set_header           Host $host;
            proxy_set_header  X-Real-IP  $remote_addr;
            proxy_set_header           X-Forwarded-For       $proxy_add_x_forwarded_for;
            client_max_body_size  100m;
      }
    
      #说明: 这里模拟静态资源读取,示例请求url: http://www.xxxx.com/app_images/xxx.png
      #访问 www.xxxx.com/app_images/xxx.png 会转发访问服务器内的绝对路径/usr/local/app_images/xxx.png
      location /app_images {
    	    root /usr/local;
      }       
    
      # 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;
      }
    }
}

2.3 访问测试

配置文件完成后切记重新启动 nginx, 我这里使用 http 配置代理端口为 8081, 所以浏览器输入域名: 8081/api(spring boot 中配置的 context-path)/ 请求 (Controller 路径)

http 方式测试

  • 访问 nginx

  • 创建静态资源文件夹, 上传一张图片进行测试

  • controller 请求路径测试


可以看到我们使用 http 方式时, 地址栏度提示的不安全链接

https: 测试

把配置文件切换成 https 方式, 重启 nginx, 目前备案还没完成, 所以我这里需要使用域名: 442 进行访问


使用 https 访问消除了不安全告警

标签: #软件开发 1171 #JAVA 991
相关文章

Spring 实现 3 种异步接口 2024-10-18 09:07

大家好,我是苏三~ 如何处理比较耗时的接口? 这题我熟,直接上异步接口,使用 Callable、WebAsyncTask 和 DeferredResult、CompletableFuture等均可实现。 但这些方法有局限性,处理结果仅返回单个值。在某些场景下,如果需要接口异步处理的同时,还持续不断地

重学SpringBoot3-集成Redis(五)之布隆过滤器 2024-10-08 11:24

更多SpringBoot3内容请关注我的专栏:《SpringBoot3》 期待您的点赞👍收藏⭐评论✍ 重学SpringBoot3-集成Redis(五)之布隆过滤器 1. 什么是布隆过滤器? * 基本概念 适用场景 2. 使用 Redis 实现布隆过滤器 * 项目依赖 Redis 配置

SpringBoot整合异步任务执行 2024-10-08 11:24

同步任务: 同步任务是在单线程中按顺序执行,每次只有一个任务在执行,不会引发线程安全和数据一致性等 并发问题 同步任务需要等待任务执行完成后才能执行下一个任务,无法同时处理多个任务,响应慢,影响用 户体验 异步任务: 异步任务是在多线程中同时执行,多个任务可以并发执行,同时处理多个请求,响应快,资源

springboot kafka多数据源,通过配置动态加载发送者和消费者 2024-10-08 11:24

前言 最近做项目,需要支持kafka多数据源,实际上我们也可以通过代码固定写死多套kafka集群逻辑,但是如果需要不修改代码扩展呢,因为kafka本身不处理额外逻辑,只是起到削峰,和数据的传递,那么就需要对架构做一定的设计了。 准备test kafka本身非常容易上手,如果我们需要单元测试,引入ja

SpringBoot 集成 Redis 2024-10-08 11:24

一:SpringBoot 集成 Redis ①Redis是一个 NoSQL(not only)数据库, 常作用缓存 Cache 使用。 ②Redis是一个中间件、是一个独立的服务器;常用的数据类型: string , hash ,set ,zset , list ③通过Redis客户端可以使用多种语

SpringBoot整合QQ邮箱 2024-10-08 11:24

SpringBoot可以通过导入依赖的方式集成多种技术,这当然少不了我们常用的邮箱,现在本章演示SpringBoot整合QQ邮箱发送邮件…. 下面按步骤进行: 1.获取QQ邮箱授权码 1.1 登录QQ邮箱 1.2 开启SMTP服务 找到下图中的SMTP服务区域,如果当前账号未开启的话自己手动开启。

目录

IT 外包服务商

  • 意见投递
  • zyf6619

软件开发应用

主菜单

  • 首页
  • 软件开发
  • 计算机基础
  • Hello Halo
  • 新手必读
  • 关于本知识库
Copyright © 2024 your company All Rights Reserved. Powered by Halo.