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


    目录

    一、Nginx配置文件

    二、跨域解决


    一、Nginx配置文件

    1. # 禁止ip访问
    2. server {
    3. ssl_certificate /ssl/xloda.com_cert_chain.pem;
    4. ssl_certificate_key /ssl/xloda.com_key.key;
    5. ssl_session_cache shared:SSL:1m;
    6. ssl_session_timeout 5m;
    7. ssl_ciphers HIGH:!aNULL:!MD5;
    8. ssl_prefer_server_ciphers on;
    9. listen 80 default_server;
    10. listen 443 ssl default_server;
    11. server_name _;
    12. #强制将http的URL重写成https
    13. return 301 https://xloda.com$request_uri;
    14. }
    15. server {
    16. listen 80;
    17. # д ֤
    18. server_name xloda.com;
    19. #charset koi8-r;
    20. # access_log logs/host.access.log main;
    21. #ǿ ƽ http URL д https
    22. return 301 https://$host$request_uri;
    23. }
    24. # HTTPS server
    25. #
    26. server {
    27. listen 443 ssl;
    28. server_name xloda.com;
    29. ssl_certificate /ssl/xloda.com_cert_chain.pem;
    30. ssl_certificate_key /ssl/xloda.com_key.key;
    31. ssl_session_cache shared:SSL:1m;
    32. ssl_session_timeout 5m;
    33. ssl_ciphers HIGH:!aNULL:!MD5;
    34. ssl_prefer_server_ciphers on;
    35. location /qiniu/ {
    36. add_header 'Access-Control-Allow-Origin' $http_origin;
    37. add_header 'Access-Control-Allow-Credentials' 'true';
    38. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS,PUT,PATCH,DELETE,HEAD';
    39. 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';
    40. add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
    41. if ($request_method = 'OPTIONS') {
    42. add_header 'Access-Control-Max-Age' 1728000;
    43. add_header 'Content-Type' 'text/plain; charset=utf-8';
    44. add_header 'Content-Length' 0;
    45. return 204;
    46. }
    47. proxy_pass http://qiniu.xloda.com/;
    48. }
    49. location / {
    50. root /www/wwwroot/out;
    51. index index.html index.htm;
    52. try_files $uri $uri.html $uri/ =404;
    53. rewrite ^//(.*)$ //$1.html break;
    54. }
    55. error_page 404 /404.html;
    56. location = /404 {
    57. internal;
    58. }
    59. }

    二、跨域解决

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

    1. location /qiniu/ {
    2. add_header 'Access-Control-Allow-Origin' $http_origin;
    3. add_header 'Access-Control-Allow-Credentials' 'true';
    4. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS,PUT,PATCH,DELETE,HEAD';
    5. 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';
    6. add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
    7. if ($request_method = 'OPTIONS') {
    8. add_header 'Access-Control-Max-Age' 1728000;
    9. add_header 'Content-Type' 'text/plain; charset=utf-8';
    10. add_header 'Content-Length' 0;
    11. return 204;
    12. }
    13. proxy_pass http://qiniu.xloda.com/;
    14. }

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

  • 相关阅读:
    分组密码与高级加密标准(三)
    gitea的git库备份与恢复
    Java SOAP 调用 C# 的WebService
    解决jsonp跨域中的安全漏洞(包含meta解释)
    EF Core修改Migration更新数据库表
    【学习笔记】项目进行过程中遇到有关composer的问题
    Linux UWB Stack实现——MCPS调度接口(API)
    【生物信息学】使用谱聚类(Spectral Clustering)算法进行聚类分析
    python类方法和静态方法区别详细讲解
    PHP安装配置
  • 原文地址:https://blog.csdn.net/qq_50909707/article/details/141096440