Nginx 配置Nextjs和SpringBoot项目的https并解决跨域问题

目录

一、Nginx配置文件

二、跨域解决


一、Nginx配置文件

复制代码
 # 禁止ip访问
    server {
        ssl_certificate      /ssl/xloda.com_cert_chain.pem;
        ssl_certificate_key  /ssl/xloda.com_key.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
    
        listen 80 default_server;
        listen 443 ssl default_server;
        server_name _;
        
        #强制将http的URL重写成https
        return 301 https://xloda.com$request_uri;
    }

    
     server {
        listen       80;
        #  д  ֤       
        server_name  xloda.com;
    
        #charset koi8-r;
    
        # access_log  logs/host.access.log  main;
    
        #ǿ ƽ http  URL  д  https
        return 301 https://$host$request_uri;
    }
    
    # HTTPS server
    #
    server {
        listen       443 ssl;
        server_name  xloda.com;

        ssl_certificate      /ssl/xloda.com_cert_chain.pem;
        ssl_certificate_key  /ssl/xloda.com_key.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
        
        location /qiniu/ {
            add_header 'Access-Control-Allow-Origin' $http_origin;
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS,PUT,PATCH,DELETE,HEAD';
            add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
            add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
            if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Max-Age' 1728000;
                add_header 'Content-Type' 'text/plain; charset=utf-8';
                add_header 'Content-Length' 0;
                return 204;
            }
        
            proxy_pass http://qiniu.xloda.com/;
        }
        
        location / {
            root   /www/wwwroot/out;
            index  index.html index.htm;
            try_files $uri $uri.html $uri/ =404;
            rewrite ^//(.*)$ //$1.html break;
        }
        
        error_page 404 /404.html;
        location = /404 {
            internal;
        }
    }

二、跨域解决

起初配置了https的前端是不能正常访问http的后端的,于是我将后端项目也配置成了https,后端数据问题得以解决,但这里的OSS为七牛云绑定的http链接,图片资源还是会报错,于是我采用了代理加跨域允许的方式解决了该问题。

复制代码
location /qiniu/ {
            add_header 'Access-Control-Allow-Origin' $http_origin;
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS,PUT,PATCH,DELETE,HEAD';
            add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
            add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
            if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Max-Age' 1728000;
                add_header 'Content-Type' 'text/plain; charset=utf-8';
                add_header 'Content-Length' 0;
                return 204;
            }
        
            proxy_pass http://qiniu.xloda.com/;
        }

这里的 xloda.com/qiniu/ 就类似于 qiniu.xloda.com/ 前端项目里将OSShost改成代理的路径即可。

相关推荐
Z21-X4 分钟前
云原生运维实战笔记:华为云计算与网络类服务全景解析
运维·笔记·学习·云原生·华为云
Sayai4 分钟前
Elasticsearch ingest pipeline 被禁用之后:性能代价、安全治理与数据入库时间的四种替代方案
大数据·运维·安全·elasticsearch·搜索引擎·性能优化
kiros_wang6 分钟前
通用工具模块封装:防抖节流、参数校验公共工具类
linux·运维·ubuntu
Android系统攻城狮17 分钟前
Linux FFmpeg深度解析之avcodec_default_get_buffer2调用流程与实战(二十)
linux·运维·服务器·ffmpeg·音视频进阶·ffmpeg音视频
深圳恒讯18 分钟前
香港云主机硬盘满了怎么办?Linux和Windows在线扩容操作教程
linux·运维·windows
饭后一颗花生米20 分钟前
GitHub Actions 自动化:5 个真实运维提效案例
运维·自动化·github
Android系统攻城狮26 分钟前
Linux FFmpeg深度解析之av_parser_init调用流程与实战(二十一)
linux·运维·服务器·ffmpeg·音视频进阶·ffmpeg音视频
Canmag 兴隆磁性1 小时前
钕铁硼磁片取向方向检测自动化方案:从人工目检到流水线在线分选的工程实践
运维·数据库·自动化·工业自动化·钕铁硼·自动化检测·磁材料
学长毕设1 小时前
基于SpringBoot的文具商城在线零售系统-附源码
spring boot·后端·零售
寺中人1 小时前
CentOS 7进程与任务管理
linux·运维·centos·服务器搭建与管理·linux运维命令·nohup screen