Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12291

NGINX Remove Subdirectory From Path

$
0
0

Preface

I want to start out by saying I know there are at least 200 different SO posts on this exact issue, i've been over every one of them, to summarize for anyone else who stumbles upon this.

Issue

I have a NGINX container (Nginx01) that is acting as the front end to a php-fpm server. I want the NGINX server to respond to http://.com/wiki/ but strip the /wiki/ part of the URL before sending it to the backend server. I want to do this specifically to avoid making unnecessary changes to the mediawiki container although I am starting to feel that would be easier at this point...

Background Information

A single volume is shared between both containers hosting the website files. PHP files are hosted through the php-fpm container, all static files should be hosted through NGINX.

Key Tips I've Found

Be mindful of the slashes (/)

Remember that proxy pass + whats captured in the "(.*)" gets combined and thats your final URL. Ensure that you are not adding too many or too little slashes. (Ex1. http://localhost//subdir | Ex2; http://localhostsubdir)

rewrite ^/wiki/(.*)$ $1 break;

Ensure you have break in the command.

Ensure that the break command is located at the end of the rewrite command to ensure it stops processing.

rewrite ^/wiki/(.*)$ $1 break;

Configuration File(s) & Log(s)


nginx.conf

...    # Build a php rewrite location    location @php_rewrite {        rewrite ^/(.*)$ /index.php;    }...

mediawiki01.conf

location /wiki {    # Redirect /wiki to /wiki/    return 301 /wiki/;}location /wiki/ {    # Define the root directory where MediaWiki resources are mounted.    alias /usr/share/nginx/html/mediawiki01/html/;    # Remove the /wiki prefix before passing the request to backend    # rewrite ^/wiki/(.*)$ $1 break;    # Proxy settings to forward requests to MediaWiki PHP-FPM (for non-PHP files or if necessary)    proxy_pass http://mediawiki01:9000/;    proxy_set_header Host $host;    proxy_set_header X-Real-IP $remote_addr;    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    proxy_set_header X-Forwarded-Proto $scheme;    proxy_http_version 1.1;    # Try to serve files directly and fallback to index.php, adjusted for rewritten URI    try_files $uri @php_rewrite;    # Allow access to approved file types only.    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {        try_files $uri /index.php;        expires max;        log_not_found off;        allow all;    }    # Block access to the /mw-config directory    location ^~ /wiki/mw-config/ {        internal;    }    # Location block to handle PHP file execution using FastCGI    location ~ \.php$ {        # Pass the PHP requests to FastCGI server listening on mediawiki01 at port 9000        # This is typically the address and port where your PHP-FPM server (MediaWiki) is running        fastcgi_pass mediawiki01:9000;        # Define the default file to serve when a directory is requested        fastcgi_index index.php;        # The regex to split the PATH_INFO part from the SCRIPT_FILENAME        # It captures the PHP file path as well as any additional path info        fastcgi_split_path_info ^(.+\.php)(/.*)$;        # Set the SCRIPT_FILENAME parameter that will be passed to FastCGI server        # It combines the document root and the script name to form the absolute path to the PHP script        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;        # Explicitly setting HTTPS off;        fastcgi_param HTTPS off;        # Include the standard parameters provided for FastCGI requests        # This file contains presets that are commonly required for FastCGI processing        include fastcgi_params;    }}

MediaWiki01 Logs

Logs are still showing that the /wiki/ is being included in the URL.

172.18.0.3 -  18/Jun/2024:10:00:04 +0000 "GET /wiki/index.php" 404

Viewing all articles
Browse latest Browse all 12291

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>