nginx跨域设置 nginx apache nginx php nginx rewrite

PHP中文网
发布: 2017-04-01 16:50:32
原创
1596人浏览过
  • 在做一个项目的时候,采用了前后端分离的结构设计,后台设计的rest风格的http接口既需要满足后台服务调用,也需要满足前端直接采用ajax调用,于是碰到了跨域问题, 后台采用的是spring mvc结构, http方法支持get、post、put、delete、option等方法,由于在post调用的时候,前端http会先采用option到服务器端,204了之后再提交请求数据,除了在nginx层面做调整之外,spring web.xml也需要设置下, 这种方案不需要在服务端增加filter之类的东西,得到的可行的版本如下:

  • spring web.xml

<servlet>
<servlet-name>root</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-web.xml</param-value>
</init-param><init-param><param-name>dispatchOptionsRequest</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet><servlet-mapping>
<servlet-name>root</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
登录后复制
  • nginx.conf 有些地方比较罗嗦

    奇域
    奇域

    奇域是一个专注于中式美学的国风AI绘画创作平台

    奇域 30
    查看详情 奇域
location ~/api/* {
            dav_methods PUT DELETE;
            if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Allow-Origin' '$http_origin';
                add_header 'Access-Control-Allow-Methods' 'GET, POST,PUT,DELETE, OPTIONS';
                add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-CustomHeader,Keep-Alive,
                User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
                add_header 'Access-Control-Max-Age' 3600;
                add_header 'Content-Type' 'text/plain charset=UTF-8';
                add_header 'Content-Length' 0;
                return 204;
            }
            if ($request_method = 'POST') {
                add_header 'Access-Control-Allow-Origin' '$http_origin';
                add_header 'Access-Control-Allow-Methods' 'GET, POST,PUT,DELETE, OPTIONS';
                add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-CustomHeader,Keep-Alive,
                User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
            }
            if ($request_method = 'GET') {
                add_header 'Access-Control-Allow-Origin' '$http_origin';
                add_header 'Access-Control-Allow-Methods' 'GET, POST,DELETE,PUT, OPTIONS';
                add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-CustomHeader,Keep-Alive,
                User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
            }
      if ($request_method = 'PUT') {
                add_header 'Access-Control-Allow-Origin' '$http_origin';
                add_header 'Access-Control-Allow-Methods' 'GET, POST,PUT,DELETE, OPTIONS';
                add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-CustomHeader,Keep-Alive,
                User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
            }
            if ($request_method = 'DELETE') {
                add_header 'Access-Control-Allow-Origin' '$http_origin';
                add_header 'Access-Control-Allow-Methods' 'GET, POST,DELETE,PUT, OPTIONS';
                add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-CustomHeader,Keep-Alive,
                User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
            }
            root /opt/www/web/;
            index index.jsp;
            proxy_pass <a rel="nofollow" href="http://localhost.html" target="_blank">
            <a rel="nofollow" href="http://localhost" target="_blank">http://localhost</a></a>:8089;
            include /opt/conf/nginx/proxy.conf;
        }
登录后复制

 以上就是nginx跨域设置 nginx apache nginx php nginx rewrite的内容,更多相关内容请关注PHP中文网(www.php.cn)!

相关标签:
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号