0

On Apache we used to have a URL like MyUrl.com/UserId and in the root index.php code executed that got the request_uri that had the UserId. In Nginx instead it looks for a folder called UserId which doesn’t exist. How can I modify the Nginx config to still use the root index.php file instead of look for a non existent folder?

Here is my current nginx config:

# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/MyUrl.com/before/*;

server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name MyUrl.com;
root /home/forge/MyUrl.com;
rewrite_log on;

# FORGE SSL (DO NOT REMOVE!)
ssl_certificate /etc/nginx/ssl/MyUrl.com/766650/server.crt;
ssl_certificate_key /etc/nginx/ssl/MyUrl.com/766650/server.key;

ssl_protocols TLSv1.2;
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384;
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/nginx/dhparams.pem;

add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";

index index.html index.htm index.php;

charset utf-8;

# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/MyUrl.com/server/*;

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt  { access_log off; log_not_found off; }

access_log /var/log/nginx/MyUrl.com-access.log combined;
error_log  /var/log/nginx/MyUrl.com-error.log error;
error_page 404 /index.php;

location ~ \.php$ {
    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_read_timeout 180;
    include fastcgi_params;
}

location ~ /\.(?!well-known).* {
    deny all;
}

location /api/v1/ {
    rewrite_log on;
    index index.php;
    fastcgi_index index.php;
    error_page  405     =200 $uri;
    
    if (!-e $request_filename){
        rewrite /api/v1/(.*)$ /api/v1/api.php?request=$1 break;
    }
}

client_max_body_size 128M;
}

# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/MyUrl.com/after/*;
2
  • Can you show how you have configured Nginx (please add the config files and not just describe it) along with the example URL that you are trying to use. Commented Jul 9, 2020 at 15:34
  • @NigelRen I have included it, thanks Commented Jul 9, 2020 at 16:03

1 Answer 1

1

Apache and Nginx use different configuration formats. You need to convert what is currently in your .htaccess file into a format that Nginx understands.

This is an example of an Apache .htaccess configuration:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

In Nginx it could look something like this:

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

Take a look at these questions for more details:

Also this tool might help with the conversion:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.