I’m trying to configure Nginx to serve my application without exposing a subdirectory in the URL. My application files are located in a subdirectory (ddformbuilder), but I want the URLs to appear as if they are served directly from the root domain without the subdirectory being visible.
Current Setup:
Domain: https://app.example.com
Application Directory: /var/app/current/ddformbuilder/
Desired URL: I want URLs like https://app.example.com/login.php instead of https://app.example.com/ddformbuilder/login.php.
Current Nginx Configuration:
files:
"/etc/nginx/conf.d/01_custom.conf":
mode: "000644"
owner: root
group: root
content: |
server {
listen 80;
server_name app.example.com;
root /var/app/current/ddformbuilder/;
index index.php;
location / {
root /ddformbuilder;
try_files $uri $uri/ /index.php?$args;
}
location ~ .php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT /var/app/current/ddformbuilder;
}
}
Even with this configuration, the URLs still include the subdirectory (ddformbuilder). When I try to access https://app.example.com/login.php, I get a “File not found” error. However, the URL https://app.example.com/ddformbuilder/login.php works fine.
I need the root URL (https://app.example.com) to serve files from /var/app/current/ddformbuilder/ without showing ddformbuilder in the URL path.
What I Tried:
- Adjusting the root directive and try_files settings.
- Testing different configurations in the location block.
- Changing Nginx configuration and reloading Nginx (sudo nginx -t confirms the configuration is okay).
Your configuration is almost correct. You just need to remove root /ddformbuilder;
from location
.
1