【问题标题】:Problem with switching PHP code from Apache to Nginx将 PHP 代码从 Apache 切换到 Nginx 的问题
【发布时间】:2020-10-30 06:18:17
【问题描述】:

在 Apache 上,我们曾经有一个类似 MyUrl.com/UserId 的 URL,并且在执行的根 index.php 代码中获得了具有 UserId 的 request_uri。相反,在 Nginx 中,它会查找一个名为 UserId 的文件夹,该文件夹不存在。如何修改 Nginx 配置以仍然使用根 index.php 文件而不是查找不存在的文件夹?

这是我当前的 nginx 配置:

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

【问题讨论】:

  • 您能否展示您如何配置 Nginx(请添加配置文件,而不仅仅是描述它)以及您尝试使用的示例 URL。
  • @NigelRen 我已经收录了,谢谢

标签: php apache nginx


【解决方案1】:

Apache 和 Nginx 使用不同的配置格式。您需要将您的 .htaccess 文件中当前的内容转换为 Nginx 可以理解的格式。

这是 Apache .htaccess 配置的示例:

Options +FollowSymLinks
RewriteEngine On

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

在 Nginx 中它可能看起来像这样:

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

查看这些问题了解更多详情:

此工具也可能有助于转换:

【讨论】:

    猜你喜欢
    • 2014-10-11
    • 2017-11-28
    • 1970-01-01
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    相关资源
    最近更新 更多