nginx跨網域設定 nginx apache nginx php nginx rewrite

來源:互聯網
上載者:User
  • 在做一個項目的時候,採用了前後端分離的結構設計,後台設計的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 有些地方比較羅嗦

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的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.