简体   繁体   中英

Problem with switching PHP code from Apache to Nginx

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/*;

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:

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/*;

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