Rumah > pembangunan bahagian belakang > tutorial php > nginx跨域设置 nginx apache nginx php nginx rewrite

nginx跨域设置 nginx apache nginx php nginx rewrite

PHP中文网
Lepaskan: 2023-02-28 21:46:02
asal
1351 orang telah melayarinya
  • 在做一个项目的时候,采用了前后端分离的结构设计,后台设计的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>
Salin selepas log masuk
  • nginx.conf 有些地方比较罗嗦

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;
        }
Salin selepas log masuk

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

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan