【问题标题】:Remove .php extension with NGINX使用 NGINX 删除 .php 扩展名
【发布时间】:2016-11-03 11:59:59
【问题描述】:

我在 NGINX 网络服务器上,我想从 url 中删除 .php 扩展。

我目前有以下配置:

server {
    server_name www.mywebsite.com mywebsite.com;
    return 301 https://mywebsite.com$request_uri;
}
server {
    listen 443 ssl;

    server_name www.mywebsite.com;
    return 301 https://mywebsite.com$request_uri;

    ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mywebsite.com/privkey.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
}
server {
    listen 443 ssl;
    root /opt/http/nginx/sites/mywebsite/www;
    index index.php index.html;
    server_name mywebsite.com;

    location / {
        #rewrite ^(/.*)\.html(\?.*)?$ $1$2 permanent;
        try_files $uri $uri/ $uri.php?$args;
    }

    location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 365d;
    }

    location ~*  \.(pdf)$ {
        expires 30d;
    }

    client_max_body_size 3M;

    ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mywebsite.com/privkey.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

    error_page 403 /index.php;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    access_log   /var/log/nginx/www.mywebsite.access.log;
    error_log   /var/log/nginx/www.mywebsite.error.log;
}

如果尝试遵循一些类似的案例说明,但我的配置似乎没有任何效果。

问题在于这段代码

location / {
    try_files $uri $uri/ $uri.php?$args;
}

在两种情况下运行良好:

  1. 客户要求https://mywebsite.com/page.php:OK
  2. 客户端请求https://mywebsite.com/page:OK

无需重写网址!

如果客户端尝试访问带有文件扩展名的页面,我需要告诉 NGINX 重写 url。例如,如果我要求 login.php,nginx 会重写'login',等等。

如果有,它还必须在 url 中保留 GET 参数。

那么正确的配置是什么,因为我在我的代码中保留了指向带有扩展名的 php 文件的链接,而不是相对 url(我希望 NGINX 可以重写它们)? (如果我需要在我的代码中设置相对 url,我可以,但我不想破坏我的本地)

【问题讨论】:

  • 是的,我按照这篇文章中的说明进行操作,但没有任何效果...
  • 只需在server {} 块中添加rewrite ^(/.+)\.php$ $scheme://$host$1 permanent;,它总是会重写没有.php 扩展名的url!

标签: php nginx url-rewriting


【解决方案1】:

感谢这个问题和 cmets,我能够找出一个类似的问题,同样的结果有一些额外的限制,比如不使用 ifs 或 try_files。这是我最后得到的:

upstream php {
    server unix:/var/run/php-fpm/php-fpm.sock;
}

server {
#    ...

rewrite ^(/.+)\.php$ $scheme://$host$1 permanent;

location / {
    rewrite ^(.*)$ /$1.php;
}

location ~ \.php$ {
    include fastcgi.conf;
    fastcgi_pass    php;
}

主要贡献者是重写中缺少 break/last。

【讨论】:

    【解决方案2】:
    location / {
            try_files $uri $uri.html $uri/ @extensionless-php;
            index index.html index.htm index.php;
        }
    
        location ~ \.php$ {
            try_files $uri =404;
        }
    
        location @extensionless-php {
            rewrite ^(.*)$ $1.php last;
        }
    

    这将删除 html 和 php 表单 url .More Detail

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-22
      • 1970-01-01
      • 2011-04-30
      相关资源
      最近更新 更多