Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建NGINX位置上下文,以允许将部分URL扩展为完整URL_Nginx_Nginx Location - Fatal编程技术网

创建NGINX位置上下文,以允许将部分URL扩展为完整URL

创建NGINX位置上下文,以允许将部分URL扩展为完整URL,nginx,nginx-location,Nginx,Nginx Location,要访问我的应用程序,您需要转到 我正在尝试配置nginx,这样,如果用户输入以下任意内容,它们将以我前面引用的完整URL结束 https://dev01.domain.com/ https://dev01.domain.com/dev https://dev01.domain.com/dev/ https://dev01.domain.com/dev/admin https://dev01.domain.com/dev/admin/ 在读了很多我能找到的东西,再加上玩了很多东西之后,我终于想出

要访问我的应用程序,您需要转到

我正在尝试配置nginx,这样,如果用户输入以下任意内容,它们将以我前面引用的完整URL结束

https://dev01.domain.com/
https://dev01.domain.com/dev
https://dev01.domain.com/dev/
https://dev01.domain.com/dev/admin
https://dev01.domain.com/dev/admin/
在读了很多我能找到的东西,再加上玩了很多东西之后,我终于想出了这个场景

server {
location = / {
        try_files $uri /dev01/admin/index.php;
}

location = /dev01 {
        try_files $uri /admin/index.php;
}


location = /dev01/ {
        try_files $uri admin/index.php;
}


location = /dev01/admin/ {
        try_files $uri /index.php;
}

location ~ \.php$ {
        try_files $uri /dev01/admin/index.php =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        }

}
虽然这似乎产生了我所期待的结果,但我认为有一种更好、更优化的处理方法

如有任何建议,将不胜感激。。。多谢各位

-----------------附加信息------------------

我完全同意你的理论。我玩过很多这样的场景。当我尝试点击时,我已经附加了我的服务器{}上下文的关键部分以及调试错误日志

似乎我们可以从$uri和$uri/中看到尝试,但是当它们失败时,我认为应该返回到“fallback”(try_文件的最后一个参数) 但它似乎尝试了不存在的index.html,然后尝试了根目录索引,当生成403时,它会在内部重定向到my/error.html位置。 它似乎从来没有试图打“退路”。。。(/dev01/admin/index.php?$query\u字符串)

--以下是尝试创建的调试日志文件

2018/08/09 11:53:40 [debug] 10318#0: *1 test location: "/"
2018/08/09 11:53:40 [debug] 10318#0: *1 test location: ~ "\.php$"
2018/08/09 11:53:40 [debug] 10318#0: *1 using configuration "/"
2018/08/09 11:53:40 [debug] 10318#0: *1 http cl:-1 max:1048576
2018/08/09 11:53:40 [debug] 10318#0: *1 rewrite phase: 3
2018/08/09 11:53:40 [debug] 10318#0: *1 post rewrite phase: 4
2018/08/09 11:53:40 [debug] 10318#0: *1 generic phase: 5
2018/08/09 11:53:40 [debug] 10318#0: *1 generic phase: 6
2018/08/09 11:53:40 [debug] 10318#0: *1 generic phase: 7
2018/08/09 11:53:40 [debug] 10318#0: *1 access phase: 8
2018/08/09 11:53:40 [debug] 10318#0: *1 access phase: 9
2018/08/09 11:53:40 [debug] 10318#0: *1 access phase: 10
2018/08/09 11:53:40 [debug] 10318#0: *1 post access phase: 11
2018/08/09 11:53:40 [debug] 10318#0: *1 generic phase: 12
2018/08/09 11:53:40 [debug] 10318#0: *1 try files handler
2018/08/09 11:53:40 [debug] 10318#0: *1 http script var: "/"
2018/08/09 11:53:40 [debug] 10318#0: *1 trying to use file: "/" "/mnt/files/"
2018/08/09 11:53:40 [debug] 10318#0: *1 http script var: "/"
2018/08/09 11:53:40 [debug] 10318#0: *1 trying to use dir: "/" "/mnt/files/"
2018/08/09 11:53:40 [debug] 10318#0: *1 try file uri: "/"
2018/08/09 11:53:40 [debug] 10318#0: *1 generic phase: 13
2018/08/09 11:53:40 [debug] 10318#0: *1 content phase: 14
2018/08/09 11:53:40 [debug] 10318#0: *1 open index "/mnt/files/index.html"
2018/08/09 11:53:40 [debug] 10318#0: *1 stat() "/mnt/files/index.html" failed (2: No such file or directory)
2018/08/09 11:53:40 [debug] 10318#0: *1 http index check dir: "/mnt/files"
2018/08/09 11:53:40 [debug] 10318#0: *1 content phase: 15
2018/08/09 11:53:40 [debug] 10318#0: *1 content phase: 16
2018/08/09 11:53:40 [debug] 10318#0: *1 content phase: 17
2018/08/09 11:53:40 [error] 10318#0: *1 directory index of "/mnt/files/" is forbidden, client: xxx.xxx.xxx.xxx, server: dev.domain.com, request: "GET / HTTP/1.1", host: "dev.domain.com"
2018/08/09 11:53:40 [debug] 10318#0: *1 http finalize request: 403, "/?" a:1, c:1
2018/08/09 11:53:40 [debug] 10318#0: *1 http special response: 403, "/?"
2018/08/09 11:53:40 [debug] 10318#0: *1 internal redirect: "/error.html?"
那么:

server {

location = / {
 rewrite "(?i)^/$" /dev01/admin/index.php permanent;
 rewrite "(?i)^/dev/?$" /dev01/admin/index.php permanent;
 rewrite "(?i)^/dev/admin/?$" /dev01/admin/index.php permanent;
}

location = /dev01/admin/ {
        try_files $uri /index.php;
}

location ~ \.php$ {
        try_files $uri /dev01/admin/index.php =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
}

}
你最好(重新)阅读上的文档

以下代码应作为用例的示例。
try\u files
部分取决于您的
root
指令。更多信息


我在显示此建议结果的主问题中添加了更多信息。请尝试删除
位置块中的
Try_files
指令,值得添加:
index index.php
;在
服务器中
块。结果相同----
server {

location = / {
 rewrite "(?i)^/$" /dev01/admin/index.php permanent;
 rewrite "(?i)^/dev/?$" /dev01/admin/index.php permanent;
 rewrite "(?i)^/dev/admin/?$" /dev01/admin/index.php permanent;
}

location = /dev01/admin/ {
        try_files $uri /index.php;
}

location ~ \.php$ {
        try_files $uri /dev01/admin/index.php =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
}

}
location / {
        try_files $uri $uri/ /dev01/admin/index.php?$query_string;
}

location ~ \.php$ {
    fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}