简体   繁体   中英

nginx api/index.php/explorer url gives 404 error

My problem is that I can not open a url which looks like this: http://myhost.mydomain.xx/.../api/index.php/explorer (This is actualy the webservice url for dolibarr crm, but thats not that important) I am using nginx with php_fastcgi. Here is the relevant part of my default site configuration from nginx:

server {
listen 80 default_server;
server_name myhost.mydomain.xx;
root /var/www/html;
index index.php index.html;
error_log     /var/log/nginx/myhost_error.log debug;
location ~ \.php$ {
#   location ~* \.php(/|$) {
#   location ~ \.php {
    try_files $uri =404;
#       try_files $uri /index.php/$uri /index.php/$uri&$args /index.php?q=$uri&$args;
#       try_files $uri /index.php/$uri;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_read_timeout 600;
    include fastcgi_params;
    access_log /var/log/nginx/php-access.log;
    error_log /var/log/nginx/php-error.log debug;
}
#   return 301 https://$host$request_uri;
    if ($lan-ip = yes) {
        set $allowed_country yes;
    }
    if ($allowed_country = no) {
        return 404;
    }

}

As you can see I have tried a couple of try_files and a couple of location directives as I googled different keywords to try to find an answer. But none works. Which is not suprising for me, because I don't really know what I am doing with the different setups.

The closest I got was with the original settings:

try_files $uri =404;

and

location ~ \.php$

but this way nginx "thinks" that the url I am trying to open is a directory.

(I have copied the nginx debug error log here for reference, maybe it's not unnecessary)

My feeling is that the location directive is badly formed, but I have to admit the regex escapes me totally.

My question is what is the correct form of a location directive, so nginx does not think that the url is a directory?

---------------- log file from here -------------------------

2018/07/02 09:57:01 [debug] 12156#12156: *2 post event 000055D561CC4740
2018/07/02 09:57:01 [debug] 12156#12156: *2 delete posted event 000055D561CC4740
2018/07/02 09:57:01 [debug] 12156#12156: *2 http wait request handler
2018/07/02 09:57:01 [debug] 12156#12156: *2 malloc: 000055D561C37B10:1024
2018/07/02 09:57:01 [debug] 12156#12156: *2 recv: fd:19 713 of 1024
2018/07/02 09:57:01 [debug] 12156#12156: *2 reusable connection: 0
2018/07/02 09:57:01 [debug] 12156#12156: *2 posix_memalign: 000055D561C494B0:4096 @16
2018/07/02 09:57:01 [debug] 12156#12156: *2 http process request line
2018/07/02 09:57:01 [debug] 12156#12156: *2 http request line: "GET /szlatest/htdocs/api/index.php/explorer HTTP/1.1"
2018/07/02 09:57:01 [debug] 12156#12156: *2 http uri: "/szlatest/htdocs/api/index.php/explorer"
2018/07/02 09:57:01 [debug] 12156#12156: *2 http args: ""
2018/07/02 09:57:01 [debug] 12156#12156: *2 http exten: ""
2018/07/02 09:57:01 [debug] 12156#12156: *2 posix_memalign: 000055D561C93350:4096 @16
2018/07/02 09:57:01 [debug] 12156#12156: *2 http process request header line
2018/07/02 09:57:01 [debug] 12156#12156: *2 http header: "Host: myhost.mydomain.xx"
2018/07/02 09:57:01 [debug] 12156#12156: *2 http header: "User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0"
2018/07/02 09:57:01 [debug] 12156#12156: *2 http header: "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
2018/07/02 09:57:01 [debug] 12156#12156: *2 http header: "Accept-Language: en-US,en;q=0.5"
2018/07/02 09:57:01 [debug] 12156#12156: *2 http header: "Accept-Encoding: gzip, deflate"
2018/07/02 09:57:01 [debug] 12156#12156: *2 http header: "Referer: http://myhost.mydomain.xx/szlatest/htdocs/api/admin/index.php?save_lastsearch_values=1&backtopage=%2Fszlatest%2Fhtdocs%2Fadmin%2Fmodules.php%3Fsearch_nature%3D%26search_version%3D%26search_status%3D"
2018/07/02 09:57:01 [debug] 12156#12156: *2 http header: "Cookie: DOLSESSID_941680c9ddfcaf66fd679287f56ff4cc=fkvdp2p8cb904h6q748r1ta391; DOLSESSTIMEOUT_7fb3b781cffd0a3d832ea68f0ebf83220634fa57=1440"
2018/07/02 09:57:01 [debug] 12156#12156: *2 http header: "Connection: keep-alive"
2018/07/02 09:57:01 [debug] 12156#12156: *2 http header: "Upgrade-Insecure-Requests: 1"
2018/07/02 09:57:01 [debug] 12156#12156: *2 http header done
2018/07/02 09:57:01 [debug] 12156#12156: *2 event timer del: 19: 1530518279502
2018/07/02 09:57:01 [debug] 12156#12156: *2 generic phase: 0
2018/07/02 09:57:01 [debug] 12156#12156: *2 rewrite phase: 1
2018/07/02 09:57:01 [debug] 12156#12156: *2 http script var
2018/07/02 09:57:01 [debug] 12156#12156: *2 http geo started: xxx.xxx.xxx.xxx
2018/07/02 09:57:01 [debug] 12156#12156: *2 http geo: no
2018/07/02 09:57:01 [debug] 12156#12156: *2 http script var: "no"
2018/07/02 09:57:01 [debug] 12156#12156: *2 http script value: "yes"
2018/07/02 09:57:01 [debug] 12156#12156: *2 http script equal
2018/07/02 09:57:01 [debug] 12156#12156: *2 http script equal: no
2018/07/02 09:57:01 [debug] 12156#12156: *2 http script if
2018/07/02 09:57:01 [debug] 12156#12156: *2 http script if: false
2018/07/02 09:57:01 [debug] 12156#12156: *2 http script var
2018/07/02 09:57:01 [debug] 12156#12156: *2 http map started
2018/07/02 09:57:01 [debug] 12156#12156: *2 http script var: "HU"
2018/07/02 09:57:01 [debug] 12156#12156: *2 http map: "HU" "yes"
2018/07/02 09:57:01 [debug] 12156#12156: *2 http script var: "yes"
2018/07/02 09:57:01 [debug] 12156#12156: *2 http script value: "no"
2018/07/02 09:57:01 [debug] 12156#12156: *2 http script equal
2018/07/02 09:57:01 [debug] 12156#12156: *2 http script equal: no
2018/07/02 09:57:01 [debug] 12156#12156: *2 http script if
2018/07/02 09:57:01 [debug] 12156#12156: *2 http script if: false
2018/07/02 09:57:01 [debug] 12156#12156: *2 test location: ~ "\.php$"
2018/07/02 09:57:01 [debug] 12156#12156: *2 using configuration ""
2018/07/02 09:57:01 [debug] 12156#12156: *2 http cl:-1 max:1048576
2018/07/02 09:57:01 [debug] 12156#12156: *2 rewrite phase: 3
2018/07/02 09:57:01 [debug] 12156#12156: *2 post rewrite phase: 4
2018/07/02 09:57:01 [debug] 12156#12156: *2 generic phase: 5
2018/07/02 09:57:01 [debug] 12156#12156: *2 generic phase: 6
2018/07/02 09:57:01 [debug] 12156#12156: *2 generic phase: 7
2018/07/02 09:57:01 [debug] 12156#12156: *2 access phase: 8
2018/07/02 09:57:01 [debug] 12156#12156: *2 access phase: 9
2018/07/02 09:57:01 [debug] 12156#12156: *2 access phase: 10
2018/07/02 09:57:01 [debug] 12156#12156: *2 post access phase: 11
2018/07/02 09:57:01 [debug] 12156#12156: *2 try files phase: 12
2018/07/02 09:57:01 [debug] 12156#12156: *2 content phase: 13
2018/07/02 09:57:01 [debug] 12156#12156: *2 content phase: 14
2018/07/02 09:57:01 [debug] 12156#12156: *2 content phase: 15
2018/07/02 09:57:01 [debug] 12156#12156: *2 content phase: 16
2018/07/02 09:57:01 [debug] 12156#12156: *2 content phase: 17
2018/07/02 09:57:01 [debug] 12156#12156: *2 http filename: "/var/www/html/szlatest/htdocs/api/index.php/explorer"
2018/07/02 09:57:01 [debug] 12156#12156: *2 add cleanup: 000055D561C4A440
2018/07/02 09:57:01 [error] 12156#12156: *2 open() "/var/www/html/szlatest/htdocs/api/index.php/explorer" failed (20: Not a directory), client: xxx.xxx.xxx.xxx, server: myhost.mydomain.xx, request: "GET /szlatest/htdocs/api/index.php/explorer HTTP/1.1", host: "myhost.mydomain.xx", referrer: "http://myhost.mydomain.xx/szlatest/htdocs/api/admin/index.php?save_lastsearch_values=1&backtopage=%2Fszlatest%2Fhtdocs%2Fadmin%2Fmodules.php%3Fsearch_nature%3D%26search_version%3D%26search_status%3D"
2018/07/02 09:57:01 [debug] 12156#12156: *2 http finalize request: 404, "/szlatest/htdocs/api/index.php/explorer?" a:1, c:1
2018/07/02 09:57:01 [debug] 12156#12156: *2 http special response: 404, "/szlatest/htdocs/api/index.php/explorer?"
2018/07/02 09:57:01 [debug] 12156#12156: *2 http set discard body
2018/07/02 09:57:01 [debug] 12156#12156: *2 xslt filter header
2018/07/02 09:57:01 [debug] 12156#12156: *2 HTTP/1.1 404 Not Found
Server: nginx/1.10.3 (Ubuntu)
Date: Mon, 02 Jul 2018 07:57:01 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Content-Encoding: gzip

2018/07/02 09:57:01 [debug] 12156#12156: *2 write new buf t:1 f:0 000055D561C93888, pos 000055D561C93888, size: 195 file: 0, size: 0
2018/07/02 09:57:01 [debug] 12156#12156: *2 http write filter: l:0 f:0 s:195
2018/07/02 09:57:01 [debug] 12156#12156: *2 http output filter "/szlatest/htdocs/api/index.php/explorer?"
2018/07/02 09:57:01 [debug] 12156#12156: *2 http copy filter: "/szlatest/htdocs/api/index.php/explorer?"
2018/07/02 09:57:01 [debug] 12156#12156: *2 image filter
2018/07/02 09:57:01 [debug] 12156#12156: *2 xslt filter body
2018/07/02 09:57:01 [debug] 12156#12156: *2 http postpone filter "/szlatest/htdocs/api/index.php/explorer?" 000055D561C4A4A0
2018/07/02 09:57:01 [debug] 12156#12156: *2 http gzip filter
2018/07/02 09:57:01 [debug] 12156#12156: *2 malloc: 000055D561C3C0C0:12288
2018/07/02 09:57:01 [debug] 12156#12156: *2 gzip alloc: n:1 s:5936 a:8192 p:000055D561C3C0C0
2018/07/02 09:57:01 [debug] 12156#12156: *2 gzip alloc: n:512 s:2 a:1024 p:000055D561C3E0C0
2018/07/02 09:57:01 [debug] 12156#12156: *2 gzip alloc: n:512 s:2 a:1024 p:000055D561C3E4C0
2018/07/02 09:57:01 [debug] 12156#12156: *2 gzip alloc: n:512 s:2 a:1024 p:000055D561C3E8C0
2018/07/02 09:57:01 [debug] 12156#12156: *2 gzip alloc: n:256 s:4 a:1024 p:000055D561C3ECC0
2018/07/02 09:57:01 [debug] 12156#12156: *2 gzip in: 000055D561C93A88
2018/07/02 09:57:01 [debug] 12156#12156: *2 gzip in_buf:000055D561C93950 ni:000055D55FD10700 ai:116
2018/07/02 09:57:01 [debug] 12156#12156: *2 malloc: 000055D561C3F0D0:4096
2018/07/02 09:57:01 [debug] 12156#12156: *2 deflate in: ni:000055D55FD10700 no:000055D561C3F0D0 ai:116 ao:4096 fl:0 redo:0
2018/07/02 09:57:01 [debug] 12156#12156: *2 deflate out: ni:000055D55FD10774 no:000055D561C3F0D0 ai:0 ao:4096 rc:0
2018/07/02 09:57:01 [debug] 12156#12156: *2 gzip in_buf:000055D561C93950 pos:000055D55FD10700
2018/07/02 09:57:01 [debug] 12156#12156: *2 gzip in: 000055D561C93A98
2018/07/02 09:57:01 [debug] 12156#12156: *2 gzip in_buf:000055D561C939A0 ni:000055D55FD10E40 ai:62
2018/07/02 09:57:01 [debug] 12156#12156: *2 deflate in: ni:000055D55FD10E40 no:000055D561C3F0D0 ai:62 ao:4096 fl:4 redo:0
2018/07/02 09:57:01 [debug] 12156#12156: *2 deflate out: ni:000055D55FD10E7E no:000055D561C3F14B ai:0 ao:3973 rc:1
2018/07/02 09:57:01 [debug] 12156#12156: *2 gzip in_buf:000055D561C939A0 pos:000055D55FD10E40
2018/07/02 09:57:01 [debug] 12156#12156: *2 free: 000055D561C3C0C0
2018/07/02 09:57:01 [debug] 12156#12156: *2 http chunk: 10
2018/07/02 09:57:01 [debug] 12156#12156: *2 http chunk: 131
2018/07/02 09:57:01 [debug] 12156#12156: *2 write old buf t:1 f:0 000055D561C93888, pos 000055D561C93888, size: 195 file: 0, size: 0
2018/07/02 09:57:01 [debug] 12156#12156: *2 write new buf t:1 f:0 000055D561C93BF8, pos 000055D561C93BF8, size: 4 file: 0, size: 0
2018/07/02 09:57:01 [debug] 12156#12156: *2 write new buf t:0 f:0 0000000000000000, pos 000055D55FD13AC0, size: 10 file: 0, size: 0
2018/07/02 09:57:01 [debug] 12156#12156: *2 write new buf t:1 f:0 000055D561C3F0D0, pos 000055D561C3F0D0, size: 131 file: 0, size: 0
2018/07/02 09:57:01 [debug] 12156#12156: *2 write new buf t:0 f:0 0000000000000000, pos 000055D55FAE3819, size: 7 file: 0, size: 0
2018/07/02 09:57:01 [debug] 12156#12156: *2 http write filter: l:1 f:1 s:347
2018/07/02 09:57:01 [debug] 12156#12156: *2 http write filter limit 0
2018/07/02 09:57:01 [debug] 12156#12156: *2 writev: 347 of 347
2018/07/02 09:57:01 [debug] 12156#12156: *2 http write filter 0000000000000000
2018/07/02 09:57:01 [debug] 12156#12156: *2 http copy filter: 0 "/szlatest/htdocs/api/index.php/explorer?"
2018/07/02 09:57:01 [debug] 12156#12156: *2 http finalize request: 0, "/szlatest/htdocs/api/index.php/explorer?" a:1, c:1
2018/07/02 09:57:01 [debug] 12156#12156: *2 set http keepalive handler
2018/07/02 09:57:01 [debug] 12156#12156: *2 http close request
2018/07/02 09:57:01 [debug] 12156#12156: *2 http log handler
2018/07/02 09:57:01 [debug] 12156#12156: *2 free: 000055D561C3F0D0
2018/07/02 09:57:01 [debug] 12156#12156: *2 free: 0000000000000000
2018/07/02 09:57:01 [debug] 12156#12156: *2 free: 000055D561C494B0, unused: 0
2018/07/02 09:57:01 [debug] 12156#12156: *2 free: 000055D561C93350, unused: 1272
2018/07/02 09:57:01 [debug] 12156#12156: *2 free: 000055D561C37B10
2018/07/02 09:57:01 [debug] 12156#12156: *2 hc free: 0000000000000000 0
2018/07/02 09:57:01 [debug] 12156#12156: *2 hc busy: 0000000000000000 0
2018/07/02 09:57:01 [debug] 12156#12156: *2 tcp_nodelay
2018/07/02 09:57:01 [debug] 12156#12156: *2 reusable connection: 1
2018/07/02 09:57:01 [debug] 12156#12156: *2 event timer add: 19: 65000:1530518286165
2018/07/02 09:57:01 [debug] 12156#12156: *2 post event 000055D561CC4740
2018/07/02 09:57:01 [debug] 12156#12156: *2 delete posted event 000055D561CC4740
2018/07/02 09:57:01 [debug] 12156#12156: *2 http keepalive handler
2018/07/02 09:57:01 [debug] 12156#12156: *2 malloc: 000055D561C37B10:1024
2018/07/02 09:57:01 [debug] 12156#12156: *2 recv: fd:19 -1 of 1024
2018/07/02 09:57:01 [debug] 12156#12156: *2 recv() not ready (11: Resource temporarily unavailable)
2018/07/02 09:57:01 [debug] 12156#12156: *2 free: 000055D561C37B10

The path info needs to be split from the URI (using the fastcgi_split_path_info directive) before the script name can be evaluated. So try_files cannot be used here, and the unfashionable if block needs to be used instead.

From the official example:

location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    if (!-f $document_root$fastcgi_script_name) {
        return 404;
    }
    ...
}

See this document for details.

I leave this in case it helps anyone. you must configure as follows. I am also using dolibarr and had the same problem

location / {

    try_files $uri $uri/ /index.php?$query_string;
    

    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        if (!-f $document_root$fastcgi_script_name) {
            return  404;
        }

        fastcgi_pass    127.0.0.1:9002;
        fastcgi_index   index.php;
        include         /etc/nginx/fastcgi_params;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM