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改成代理的路径即可。

相关推荐
武子康25 分钟前
Java-72 深入浅出 RPC Dubbo 上手 生产者模块详解
java·spring boot·分布式·后端·rpc·dubbo·nio
止观止4 小时前
React虚拟DOM的进化之路
前端·react.js·前端框架·reactjs·react
core5124 小时前
prometheus+grafana接入nginx实战
nginx·grafana·prometheus·监控·接入·vts·vtx
saynaihe4 小时前
ubuntu 22.04 anaconda comfyui安装
linux·运维·服务器·ubuntu
企鹅与蟒蛇4 小时前
Ubuntu-25.04 Wayland桌面环境安装Anaconda3之后无法启动anaconda-navigator问题解决
linux·运维·python·ubuntu·anaconda
小蜜蜂爱编程4 小时前
ubuntu透网方案
运维·服务器·ubuntu
AI视觉网奇5 小时前
git 访问 github
运维·开发语言·docker
G_whang5 小时前
jenkins使用Jenkinsfile部署springboot+docker项目
spring boot·docker·jenkins
头发那是一根不剩了5 小时前
nginx:SSL_CTX_use_PrivateKey failed
运维·服务器
七夜zippoe6 小时前
破解 VMware 迁移难题:跨平台迁移常见问题及自动化解决方案
运维·自动化·vmware