首頁  >  文章  >  後端開發  >  nginx跨域設定 nginx apache nginx php nginx rewrite

nginx跨域設定 nginx apache nginx php nginx rewrite

PHP中文网
PHP中文网原創
2017-04-01 16:50:321340瀏覽
  • 在做一個專案的時候,採用了前後端分離的結構設計,後台設計的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>
    spring web.xml
  • location ~/api/* {
                dav_methods PUT DELETE;
                if ($request_method = &#39;OPTIONS&#39;) {
                    add_header &#39;Access-Control-Allow-Origin&#39; &#39;$http_origin&#39;;
                    add_header &#39;Access-Control-Allow-Methods&#39; &#39;GET, POST,PUT,DELETE, OPTIONS&#39;;
                    add_header &#39;Access-Control-Allow-Headers&#39; &#39;Authorization,DNT,X-CustomHeader,Keep-Alive,
                    User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type&#39;;
                    add_header &#39;Access-Control-Max-Age&#39; 3600;
                    add_header &#39;Content-Type&#39; &#39;text/plain charset=UTF-8&#39;;
                    add_header &#39;Content-Length&#39; 0;
                    return 204;
                }
                if ($request_method = &#39;POST&#39;) {
                    add_header &#39;Access-Control-Allow-Origin&#39; &#39;$http_origin&#39;;
                    add_header &#39;Access-Control-Allow-Methods&#39; &#39;GET, POST,PUT,DELETE, OPTIONS&#39;;
                    add_header &#39;Access-Control-Allow-Headers&#39; &#39;Authorization,DNT,X-CustomHeader,Keep-Alive,
                    User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type&#39;;
                }
                if ($request_method = &#39;GET&#39;) {
                    add_header &#39;Access-Control-Allow-Origin&#39; &#39;$http_origin&#39;;
                    add_header &#39;Access-Control-Allow-Methods&#39; &#39;GET, POST,DELETE,PUT, OPTIONS&#39;;
                    add_header &#39;Access-Control-Allow-Headers&#39; &#39;Authorization,DNT,X-CustomHeader,Keep-Alive,
                    User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type&#39;;
                }
          if ($request_method = &#39;PUT&#39;) {
                    add_header &#39;Access-Control-Allow-Origin&#39; &#39;$http_origin&#39;;
                    add_header &#39;Access-Control-Allow-Methods&#39; &#39;GET, POST,PUT,DELETE, OPTIONS&#39;;
                    add_header &#39;Access-Control-Allow-Headers&#39; &#39;Authorization,DNT,X-CustomHeader,Keep-Alive,
                    User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type&#39;;
                }
                if ($request_method = &#39;DELETE&#39;) {
                    add_header &#39;Access-Control-Allow-Origin&#39; &#39;$http_origin&#39;;
                    add_header &#39;Access-Control-Allow-Methods&#39; &#39;GET, POST,DELETE,PUT, OPTIONS&#39;;
                    add_header &#39;Access-Control-Allow-Headers&#39; &#39;Authorization,DNT,X-CustomHeader,Keep-Alive,
                    User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type&#39;;
                }
                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.conf 有些地方比較羅嗦

🎜rrreee🎜 我(www.php.cn)! 🎜🎜
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn