带ThymeLeaf错误的Spring Boot:循环视图路径:将再次发回当前处理程序URL。(Spring Boot with ThymeLeaf error: Circular view path: would dispatch back to the current handler URL again. Check your ViewResolver setup)
我正在尝试使用Spring Boot和Thymeleaf建立一个基本项目,并将RequestMapping添加到基本的Thymeleaf模板中。 但是,我遇到了标题中的错误消息,我不知道如何解决。
我首先使用Spring Boot Initializer通过web(spring-boot-starter-web)和ThymeLeaf(spring-boot-starter-thymeleaf)依赖关系创建一个新项目。 在下载Maven项目并运行
mvn spring-boot:run
,我能够成功启动我的服务器。接下来,我尝试将一个RequestMapping添加到ThymeLeaf模板。 我使用以下代码在
src/main/java/com/example
创建了一个名为GreetingController
的新控制器:package com.example; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class GreetingController { @RequestMapping("/greeting") public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) { model.addAttribute("name", name); return "greeting"; } }
此外,在
src/main/resources/templates
,我添加了基本的greeting.html
模板:<!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Getting Started: Serving Web Content</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <p th:text="'Hello, ' + ${name} + '!'" /> </body> </html>
现在,当我重新运行服务器时,在尝试转到
localhost:8080/greeting
时出现以下异常localhost:8080/greeting
:javax.servlet.ServletException: Circular view path [greeting]: would dispatch back to the current handler URL [/greeting] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.) at org.springframework.web.servlet.view.InternalResourceView.prepareForRendering(InternalResourceView.java:205) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:145) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1271) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1037) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:980) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.11.jar:8.5.11] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474) [tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349) [tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783) [tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798) [tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1434) [tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.11.jar:8.5.11] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1153) [na:1.8.0] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [na:1.8.0] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.11.jar:8.5.11] at java.lang.Thread.run(Thread.java:785) [na:1.8.0]
看起来问题在于Thymeleaf没有得到适当的承认或某种东西,尽管它被添加为pom中的依赖。 从我在其他线程在线阅读的内容可以看出,错误发生是因为Spring认为
/greeting
路径应该指向自己,因为return "greeting";
该方法结束时的声明。 但是,我的理解是,这实际上应该是对greeting.html
(ThymeLeaf模板)的引用。我究竟做错了什么?
I'm trying to get a basic project using Spring Boot and Thymeleaf set up, and add a RequestMapping to a basic Thymeleaf template. However, I'm running into the error message in the title which I'm not sure how to resolve.
I first used the Spring Boot Initializer to create a new project with web (spring-boot-starter-web) and ThymeLeaf (spring-boot-starter-thymeleaf) dependencies. After downloading the Maven project and running
mvn spring-boot:run
, I was able to successfully start my server.Next, I tried to add a RequestMapping to a ThymeLeaf template. I created a new controller called
GreetingController
insrc/main/java/com/example
with the following code:package com.example; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class GreetingController { @RequestMapping("/greeting") public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) { model.addAttribute("name", name); return "greeting"; } }
Additionally, under
src/main/resources/templates
, I added my basicgreeting.html
template:<!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Getting Started: Serving Web Content</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <p th:text="'Hello, ' + ${name} + '!'" /> </body> </html>
Now, when I re-run the server, I get the following exception when trying to go to
localhost:8080/greeting
:javax.servlet.ServletException: Circular view path [greeting]: would dispatch back to the current handler URL [/greeting] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.) at org.springframework.web.servlet.view.InternalResourceView.prepareForRendering(InternalResourceView.java:205) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:145) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1271) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1037) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:980) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.11.jar:8.5.11] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) ~[tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474) [tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349) [tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783) [tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798) [tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1434) [tomcat-embed-core-8.5.11.jar:8.5.11] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.11.jar:8.5.11] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1153) [na:1.8.0] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [na:1.8.0] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.11.jar:8.5.11] at java.lang.Thread.run(Thread.java:785) [na:1.8.0]
It seems like the issue is that Thymeleaf is not being recognized properly or something, despite it being added as a dependency in the pom. From what I read in other threads online, the error is happening because Spring thinks that the
/greeting
path should direct to itself, due to thereturn "greeting";
statement at the end of the method. However, my understanding is that this is actually supposed to be a reference togreeting.html
, the ThymeLeaf template.What exactly am I doing wrong?
原文:https://stackoverflow.com/questions/41966454
最满意答案
好吧,我尝试了很多配置,最后一个成功了。 主要的学习不是将基本身份验证放在服务器块中,而是使用位置块。 然后不使用外部调用php,而是使用嵌套块,因此可以继承限制。
下面我将展示一个只做回声的结构来做Job,你只需要用这种风格修改你的配置
events { worker_connections 1024; } http { server { listen 80; satisfy any; location / { auth_basic "Restricted"; auth_basic_user_file /var/www/html/.htpasswd; echo "Restricted URL"; location /api/ { auth_basic "off"; echo "you reach /api"; location ~ \.php$ { echo "You reached unauthenticated php"; } } location ~ \.php$ { echo "You reached authenticated php"; } } location ~ \.php$ { echo "You reached php"; } } }
以下是我的测试
$ curl -u tarun:tarun localhost/abc/test.php You reached authenticated php $ curl -u tarun:tarun localhost/test.php You reached authenticated php $ curl localhost/test.php <html> <head><title>401 Authorization Required</title></head> <body bgcolor="white"> <center><h1>401 Authorization Required</h1></center> <hr><center>openresty/1.11.2.2</center> </body> </html> $ curl localhost/api/test.php You reached unauthenticated php $ curl localhost/api/test you reach /api
Well I tried lots of configs and finally one worked out. The main learning was not to put basic auth in server block, rather use location blocks. And then instead of calling php outside, use nested blocks, so restrictions can be inherited.
Below I will showcase a echo only structure which does the Job, you just need to modify your config in this style
events { worker_connections 1024; } http { server { listen 80; satisfy any; location / { auth_basic "Restricted"; auth_basic_user_file /var/www/html/.htpasswd; echo "Restricted URL"; location /api/ { auth_basic "off"; echo "you reach /api"; location ~ \.php$ { echo "You reached unauthenticated php"; } } location ~ \.php$ { echo "You reached authenticated php"; } } location ~ \.php$ { echo "You reached php"; } } }
Below are my test
$ curl -u tarun:tarun localhost/abc/test.php You reached authenticated php $ curl -u tarun:tarun localhost/test.php You reached authenticated php $ curl localhost/test.php <html> <head><title>401 Authorization Required</title></head> <body bgcolor="white"> <center><h1>401 Authorization Required</h1></center> <hr><center>openresty/1.11.2.2</center> </body> </html> $ curl localhost/api/test.php You reached unauthenticated php $ curl localhost/api/test you reach /api
相关问答
更多-
子文件夹中的nginx资产(nginx assets in subfolders)[2022-09-24]
你的配置几乎是正确的(对于你想要实现的目标)。 由于我没有完整的配置,我猜一个解决方案: location ^~ /assets/ { alias /absolute/path/to/assets/; gzip_static on; expires max; add_header Cache-Control public; } 注意alias指令(以及绝对路径上的尾部斜杠)。 这告诉NGINX获取URI并递归资源目录(在子文件夹中搜索)。 它类似于在提供内容时使用root和 ... -
十分简单 location / { limit_req zone=admin burst=5 nodelay; limit_req_status 503; try_files $uri $uri/ /index.php?$query_string; auth_basic "Restricted Content"; auth_basic_user_file /etc/nginx/.htpasswd; } ...
-
我将加载与vagrant作为所有者和组的共享文件夹,然后在/etc/php5/fpm/pool.d/www.conf中将用户和组更改为vagrant 。 要更改php-fpm配置中的用户和组,只需将这些添加到provision/setup.sh末尾的行: sed -i 's/user = www-data/user = vagrant/g' /etc/php5/fpm/pool.d/www.conf sed -i 's/group = www-data/group = vagrant/g' /etc/php ...
-
Nginx验证整个文件夹(Nginx auth off whole folder)[2024-02-01]
好吧,我尝试了很多配置,最后一个成功了。 主要的学习不是将基本身份验证放在服务器块中,而是使用位置块。 然后不使用外部调用php,而是使用嵌套块,因此可以继承限制。 下面我将展示一个只做回声的结构来做Job,你只需要用这种风格修改你的配置 events { worker_connections 1024; } http { server { listen 80; satisfy any; location / { au ... -
运行PHP-FPM池的用户应该在该目录中具有写权限,并且该目录应该位于PHP配置文件的open_basedir变量中。 最后,在编写上传表单之前,您应该检查阻止人们通过 PHP图像上传表单的 形式和风险 上传恶意PHP文件 。 The user that the PHP-FPM pool runs as should have write permission in that directory, and that directory should be in the open_basedir variab ...
-
您可以在/administration/ block中重复location ~ \.php$ block。 作为一种解决方法,我也成功地使用了这个设置,这使我免于在复杂场景中反复重复PHP配置。 location ^~ /administration/ { auth_basic "Restricted Area"; auth_basic_user_file /var/www/myproject/sec/htpasswd; location ~ \.php$ { ...
-
问题是localhost服务器在我配置的相同IP上进行操作,可访问被重定向到显然不可能(或者至少我不知道如何)。 我已经重定向到127.0.0.2(在/ etc / hosts中更改为127.0.0.2 accessible ),这是本地计算机服务的另一个地址,并且在nginx配置文件accessible小变化之后允许所有LAN IP地址( allow 192.168.1.0/32; )一切正常(对我本地机器和网络上的计算机)。 The problem is that localhost server op ...
-
您需要启用目录列表。 https://nginx.org/en/docs/http/ngx_http_autoindex_module.html server{ listen 80; server_name datas.test.nx; root /opt/datas/logs; location / { autoindex on; } } You need to enable directory listing. https://nginx.or ...
-
您不需要在routes.php使用任何其他配置,如果您按照本指南操作,一切都会正常工作: 最简单的解决方案是创建名为api_token列是users表。 然后,当尝试从Android设备访问资源时,只需将?api_token=
到您的URL,其中 是您的users表中的api_token列。 例如: domain.com/storage/Asset/Media/1/2?api_token=123hello4secret 系统将尝试使用api_token == 123hello4sec ... -
使用.htpasswd密码保护Nginx中文件夹中的文件(Password protecting files in a folder in Nginx using .htpasswd)[2022-04-16]
您的正则表达式语法几乎是好的,您实际上必须混合您所做的两次尝试: location "~^/admin/.*" { try_files $uri $uri/ =404; auth_basic "Restricted"; auth_basic_user_file /etc/nginx/.htpasswd; } 这是因为在正则表达式语法中. 指任何字符(单个字符), *表示之前“某事”的任何数量(可以是零)。 混合两个( .* )将表示任何字符的数量。 这是正则 ...