NGINX try_files,名称为$ uri中的最后一个单词 [英] NGINX try_files with name as the last word in the $uri

查看:391
本文介绍了NGINX try_files,名称为$ uri中的最后一个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器上有一个Nginx,我正在尝试使其打开地址为/config/www/pp1/index.php '://example.com/pp1"rel =" nofollow noreferrer> https://example.com/pp1 和"/config/www/interpreter/index.html " https://example.com/interpreter .此外,诸如 https://example.com/interpreter/res/docs 之类的东西都应该启动"/config/www/interpreter/res/docs.html ".我做了很多尝试.当前,我在/site-confs中的默认配置文件如下所示:

I've got an nginx on my server and I am trying to get it to open the file '/config/www/pp1/index.php' for address https://example.com/pp1 and '/config/www/interpreter/index.html' for https://example.com/interpreter. Furthermore all things like https://example.com/interpreter/res/docs should fire up '/config/www/interpreter/res/docs.html'. I have made many attempts. Currently my default config file in /site-confs looks like this:

server {
        listen 80;
        listen 443 ssl http2;
        server_name kni.mini.pw.edu.pl;

        # Path for SSL config/key/certificate
        ssl_certificate /config/keys/cert.crt;
        ssl_certificate_key /config/keys/cert.key;

        location / {
                proxy_read_timeout    90;
                proxy_connect_timeout 90;
                proxy_redirect        off;
                proxy_pass http://kni_website;
                proxy_set_header      X-Real-IP $remote_addr;
                proxy_set_header      X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header      Host $host;
        }

         location /pp1 {
                root /config/www;
                index index.html index.htm index.php;
                try_files $uri $uri/ index.php $uri/index.php /config/www/pp1/index.php index.php; #/index.php?$args =404;
                location ~ \.php$ {
                        fastcgi_split_path_info ^(.+\.php)(/.+)$;
                        # With php5-cgi alone:
                        fastcgi_pass 127.0.0.1:9000;
                        # With php5-fpm:
                        #fastcgi_pass unix:/var/run/php5-fpm.sock;
                        fastcgi_index index.php;
                        include /etc/nginx/fastcgi_params;
                }
        }
         location /interpreter {
                if ($request_uri ~* "([^/]*$)" ) {
                        set  $last_path_component  $1;
                }
                root /config/www;
                index index.html index.htm index.php $last_path_component.html;
                try_files /interpreter/res/$last_path_component.html $uri.html $uri.html $uri $uri/ /index.html index.php /$uri.html  /index.php?$args =404;
                location ~ \.php$ {
                        fastcgi_split_path_info ^(.+\.php)(/.+)$;
                        # With php5-cgi alone:
                        fastcgi_pass 127.0.0.1:9000;
                        # With php5-fpm:
                        #fastcgi_pass unix:/var/run/php5-fpm.sock;
                        fastcgi_index index.php;
                        include /etc/nginx/fastcgi_params;
                }
        }

}

server {
        listen 80 default_server;

        listen 443 ssl;

        root /config/www;
        index index.html index.htm index.php;

        server_name _;

        ssl_certificate /config/keys/cert.crt;
        ssl_certificate_key /config/keys/cert.key;

        client_max_body_size 0;

        location / {
                try_files $uri $uri/ /index.html /index.php?$args =404;
        }

        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                # With php5-cgi alone:
                fastcgi_pass 127.0.0.1:9000;
                # With php5-fpm:
                #fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include /etc/nginx/fastcgi_params;

        }
}

坦率地说,我以为我知道自己在做什么,但是现在我可以肯定,根本没有检查位置块 location/interpreter location的内部/pp1 导致一些疯狂的庞然大物.

Quite frankly, I thought I knew what I was doing, however right now I am pretty certain that the location block location /interpreter is not being checked at all and the insides of location /pp1 are causing some crazy mumbo-jumbo.

请帮助有需要的新手!

推荐答案

主要问题是 try_files 将在当前上下文中处理其 file 元素,因此您无法在同一条语句中处理您的 .html .php URI.有关详细信息,请参见此文档.

The main problem is that try_files will process its file elements within the current context, so you cannot handle your .html and .php URIs in the same statement. See this document for details.

一种解决方案是使用命名位置将 try_files 语句拆分为两个.首先测试 $ uri $ uri.html $ uri/index.html ,然后第二次测试 $ uri.php $ uri/index.php .

One solution is to use a named location to split the try_files statement into two. First to test $uri, $uri.html and $uri/index.html, then a second to test $uri.php and $uri/index.php.

例如:

root /path/to/root;

location /foo {
    try_files $uri $uri.html $uri/index.html @php;
    location ~* ^(.*)\.php$ { return 301 $1; }
}
location @php {
    try_files $uri.php $uri/index.php =404;
    fastcgi_pass  ...;
    ...
}

添加了 location〜* ^(.*)\.php $ 块,以正确处理以 .php 结尾的URI.最简单的解决方案是将其重定向到删除了 .php 的URI.

The location ~* ^(.*)\.php$ block is added to handle URIs that end with .php correctly. The simplest solution is to redirect them to a URI with the .php removed.

这篇关于NGINX try_files,名称为$ uri中的最后一个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆