nginx, how do I debug which location directive was applied?

Asked 1 years ago, Updated 1 years ago, 100 views

Installing mediawiki and configuring nginx for short URL.

https://www.mediawiki.org/wiki/Manual:Short_URL/Nginx/ja

Based on the official sample code (Manual:Short URL/Nginx/ja) Set /etc/nginx/conf.d/mediawiki.conf as follows:

server{
    server_name example.com;
    root/home/taro/mediawiki;

    # Location for wiki's entry points
    location~^/(index|load|api|thumb|opensearch_desc|rest|img_auth)\.php${
        include/etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_FILENAME$ document_root$fastcgi_script_name;
        fastcgi_pass 127.0.0.1:9000;
    }
    
    # Images
    location/w/images{
        # Separate location for images/so.php execution won't apply
    }
    location/w/images/deleted {
        # Deny access to deleted images folder
        deny all;
    }
    # MediaWiki associates (usually images)
    location~^/w/resources/(assets|lib|src){
        try_files$uri404;
        add_header Cache-Control "public";
        expires7d;
    }
    # Assets, scripts and styles from skins and extensions
    location~^/w/(skins|extensions)/.+\.(css|js|gif|jpg|jpeg|png|svg|wasm)${
        try_files$uri404;
        add_header Cache-Control "public";
        expires7d;
    }
    # Favicon
    location=/favicon.ico{
        alias/w/images/6/64/Favicon.ico;
        add_header Cache-Control "public";
        expires7d;
    }

    # License and credits files
    location~^/w/(COPYING|CREDITS) {
        default_type text/plain;
    }

    
    # Handling for Mediawiki REST API, see [[mw:API:REST_API]]
    location/w/rest.php/{
        try_files$uri$uri/w/rest.php?$query_string;
    }

    ## Uncomment the following code for handling image authentication
    ## Also add "deny all;" in the location for /w/images above
    # location /w/img_auth.php/{
    #   try_files$uri$uri/w/img_auth.php?$query_string;
    #}

    # Handling for the article path (pretty URLs)
    location/wiki/{
        rewrite.*/index.php;
    }

    # Allow robots.txt in case you have one
    location=/robots.txt{
    }
    # Explicit access to the root website, redirect to main page (adapt as needed)
    location=/{
        return301/wiki/main page;
    }

    # Every other entry point will be disabled.
    # Add specific rules for other entry points/images as needed above this
    location / {
        return 404;
    }
}


in your browser http://example.com

when accessing the http://example.com/wiki/ main page
You are redirected to the

http://example.com/index.php?title=Main Page
The exact same page appears when you access .

I'm deciphering the mediawiki.conf configuration, but I don't understand why it works like that.
I think there are multiple internal redirects, but I would like to know which location directives are applied to see if my understanding and nginx movement match.

My current understanding is as follows.


in your browser http://example.com
Accessing the

#Explicit access to the root website, redirect to main page (adapt as needed)
    location=/{
        return301/wiki/main page;
    }


is applied. http://example.com/wiki/ main page
You are redirected to .
(The URL on the browser will also be http://example.com/wiki/ main page)

Further

#Handling for the article path (pretty URLs)
    location/wiki/{
        rewrite.*/index.php;
    }


is applied. http://example.com/index.php
is internally redirected to .
(/Main Page information disappears?)

The display on the browser remains http://example.com/wiki/ main page.

Further

#Location for wiki's entry points
    location~^/(index|load|api|thumb|opensearch_desc|rest|img_auth)\.php${
        include/etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_FILENAME$ document_root$fastcgi_script_name;
        fastcgi_pass 127.0.0.1:9000;# or whatver port your PHP-FPM lists on
    }


is applied. /home/taro/mediawiki/index.php
runs on cgi.
(What happened to the /main page information?)

The same page will be displayed at the following two URLs:
http://example.com/wiki/ main page
http://example.com/index.php?title=Main Page

The path /wiki/main page is
?title=Main page converted to query
I think it has been handed over to /home/taro/mediawiki/index.php, but I don't know why.

#Handling for the article path (pretty URLs)
    location/wiki/{
        rewrite.*/index.php;
    }


because the information on the /main page disappears through the Is it wrong to understand that you are going through this location directive in the first place?

Therefore, I would like to trace exactly which location directives are applied in the order in which nginx internal redirects are applied.

Is it possible to write printf(), console.log(), or echo in the .conf of nginx and leave a message in the log?

/var/log/nginx/access.log
When I checked
/wiki/Main Page
I think only access to is recorded.
(There are other detailed logs, but probably just css, images, etc.)

php regular-expression nginx url cgi

2022-09-30 14:48

3 Answers

I think the path /wiki/main page has been converted to a query ?title=main page and passed to /home/taro/mediawiki/index.php, but I don't know why.

The truth is that REQUEST_URI automatically extracts the PATH_INFO equivalent (main page).

Release Notes/1.20-MediaWiki

Configuration changes

$wgUsePathInfo=true;is no longer needed to make $wgArticlePathwork on servers using like nginx, lighttpd, and apache over fastcgi.MediaWiki now always extract paths

This may be why the PATH_INFO configuration was removed from the old nginx.conf mentioned in the answer to the previous question.

https://www.mediawiki.org/w/index.php?title=Manual:Short_URL/Nginx&oldid=3103792

 location/wiki/{
    rewrite^/wiki/(?<pagename>.*)$/w/index.php;
    include/etc/nginx/fastcgi_params;
    # article path should always be passed to index.php
    fastcgi_param SCRIPT_FILENAME$ document_root/w/index.php;
    fastcgi_param PATH_INFO$pagename;
    fastcgi_param QUERY_STRING$query_string;
    fastcgi_pass 127.0.0.1:9000;# or whatver port your PHP-FPM lists on
}


2022-09-30 14:48

Three location directives, one at a time, were commented out and experimented.

#Explicit access to the root website, redirect to main page (adapt as needed)
location=/{
  return301/wiki/main page;
}
#Handling for the article path (pretty URLs)
location/wiki/{
  rewrite.*/index.php;
}
#Location for wiki's entry points
location~^/(index|load|api|thumb|opensearch_desc|rest|img_auth)\.php${
  include/etc/nginx/fastcgi_params;
  fastcgi_param SCRIPT_FILENAME$ document_root$fastcgi_script_name;
  fastcgi_pass 127.0.0.1:9000;# or whatver port your PHP-FPM lists on
}

Comment out only location=/.

http://example.com to 404 Not Found.
Redirect to http://example.com/wiki/ main page does not occur.

http://example.com/wiki/ main page is displayed successfully.
http://example.com/index.php?title=Main Page displays the main page successfully.

Comment out only location/wiki/.

http://example.com is
404 Not Found after redirecting to http://example.com/wiki/ main page.

http://example.com/wiki/ main page is also 404 Not Found.

http://example.com/index.php?title=main page displays the page.

location~^/(index|load|api|thumb|opensearch_desc|rest|img_auth)\.php$ only.

http://example.com
http://example.com/wiki/ main page
http://example.com/index.php?title=Main Page
404 Not Found for all three URLs.

The order in which the location directives are applied is

There is no doubt about the order in
(Because the number of URLs increasing gradually becomes 404 Not Found)

In addition to observing symptoms while commenting out,
Please let me know if anyone knows how to clearly understand which directives were applied in which order.

The redirected URL is

http://example.com
↓ External Redirect
http://example.com/wiki/ main page
↓ Internal Redirect
http://example.com/index.php?title=Main Page

I thought it was a movement like this, but

http://example.com
↓ External Redirect
http://example.com/wiki/ main page
↓ Internal Redirect
http://example.com/index.php

It seems to be moving like this

REQUEST_URI contains the text /wiki/main page and appears to be used in index.php.
$_SERVER['REQUEST_URI'] is not used in the index.php source, but it appears to be used in one of the files require in index.php.

Finally
If you are internally redirected to http://example.com/index.php,
The value of REQUEST_URI should be /index.php?
Why is the intermediate /wiki/main page adopted?

Can someone tell me how the REQUEST_URI is determined if it is redirected multistagely?
I think it's probably the difference between external and internal redirect, but I can't find the document on my own.


2022-09-30 14:48

How to print any log message to nginx.

/etc/nginx/nginx.conf
Added one line below to .

log_format mydebug"[MYDEBUG][$time_local]$request_uri,dbg1:$dbg1dbg2:$dbg2dbg3:$dbg3";

(mydebug or $dbg1 is any string)

/etc/nginx/conf.d/mediawiki.conf
Add set~~ and access_log~~ to .

#Location for wiki's entry points
location~^/(index|load|api|thumb|opensearch_desc|rest|img_auth)\.php${

  set$dbg1$msec;
  access_log/var/log/nginx/mydebug.log mydebug;

  include/etc/nginx/fastcgi_params;
  fastcgi_param SCRIPT_FILENAME$ document_root$fastcgi_script_name;
  fastcgi_pass 127.0.0.1:9000;# or whatver port your PHP-FPM lists on
}
#Handling for the article path (pretty URLs)
location/wiki/{

  set$dbg2$msec;
  access_log/var/log/nginx/mydebug.log mydebug;

  rewrite.*/index.php;
}
#Explicit access to the root website, redirect to main page (adapt as needed)
location=/{

  set$dbg3$msec;
  access_log/var/log/nginx/mydebug.log mydebug;

  return301/wiki/main page;
}

Check mydebug.log to see the following logs:

[MYDEBUG] [17/Jun/2021:09:51:09+0900] /,dbg1:dbg2:dbg3:1623891069.090
[MYDEBUG][17/Jun/2021:51:09+0900]/wiki/%E3%83%A1%E3%82%E3%E3%83%B3%E3%83%E3%83%BC%E3%82%B8,dbg1:1623891069.100dbg2:1623891069.100dbg3:

$request_uri is /,
dbg3 logs with timestamps first.

The location=/ directive is confirmed to have been applied first.

$request_uri is logged in /wiki/%E3%83%A1%E3%82%A4%E3%E3%83%B3%E3%83%E3%83%B8 (/wiki/main page), dbg1 and dbg2.

The dbg1 and dbg2 timestamps are the same.
dbg1 or dbg2 I don't know which comes first, but it seems to be processed continuously.

I expected 3 lines of log to be logged, but only 2 lines.

Commenting out the access_log~ of dbg2 does not affect the log output.
Only access_log~ on the dbg1 side is adopted, and access_log~ on the dbg2 side is not used.

For rewrite (internal redirect), access_log is likely to be logged in the location directive that finally arrived.

http://example.com

http://example.com/wiki/ main page

http://example.com/index.php

It was a mistake to recognize that it was a series of actions.

Browser:Request to http://example.com
Server: http://example.com/wiki/ main page response.

This is the end of the first request.

Browser: Request to http://example.com/wiki/ main page
Server: I managed to respond to html.

In the second request, REQUEST_URI becomes /wiki/main page because you are accessing http://example.com/wiki/ main page.


2022-09-30 14:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.