如何在nginx配置中删除.php扩展名?

时间:2016-02-18 20:12:09

标签: php nginx

我知道关于这个话题有很多线索,但似乎没有一个对我有用。我不确定是不是因为我必须对两个服务器块都这样做,或者我做错了什么。请帮帮我。

下面是亚马逊上远程服务器上的nginx配置,第一个块代表后端,第二个块代表前端:

limit_conn_zone $binary_remote_addr zone=conn_limit_per_ip:60m; # nginx 1.1.9 or higher
limit_req_zone $binary_remote_addr zone=req_limit_per_ip:60m rate=20r/s;

server {
    listen 8080;

    server_name api.commonskudev.com;

    root /var/www/api/public;

    gzip on;

    server_tokens off;

    index index.php;

    client_max_body_size 64M;
    fastcgi_read_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_buffers 16 16k;
    fastcgi_buffer_size 32k;
    send_timeout 300;

    try_files $uri $uri/ @rewrite-staging;

    location @rewrite-staging {
        rewrite ^ /index.php;
    }

    location ~* \.php$ {
        include fastcgi_params;
#        fastcgi_param HTTPS on;
        fastcgi_pass 127.0.0.1:9000;
    }
}

server {
    listen 443 default_server ssl;
    server_name rightsleeve.commonskudev.com;
    # rewrite ^ http://commonskudev.com/maintenance.html;

    ssl on;
    # ssl_certificate /etc/ssl/certs/ssl-cert-snakeoil.pem;
    # ssl_certificate_key /etc/ssl/private/ssl-cert-snakeoil.key;
    ssl_certificate /etc/nginx/csku-dev.crt;
    ssl_certificate_key /etc/nginx/csku-dev.key;

    gzip on;
    gzip_proxied any;

    client_max_body_size 64M;
    fastcgi_read_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_buffers 16 16k;
    fastcgi_buffer_size 32k;
    send_timeout 300;

    root /var/www/web;

    server_tokens off;

    index index.php;

    error_page 404 /404.php;

    rewrite ^/v[0-9]+\.[0-9]+/(.*)$ /$1;

    rewrite ^/project/([0-9]+) /project.php?job_number=$1;

    if ($http_referer ~* "semalt\.com") {
        return 444;
    }

    location ~* ^(css|js|images|files) {
        expires 1y;
        add_header Pragma public;
        add_header Cache-Control public;
    }

    location ~* \.(ttf|woff) {
        add_header Access-Control-Allow-Origin "*";
    }

    location ~* \.php$ {
        if (!-f $document_root/$fastcgi_script_name) {
            return 404;
        }

        limit_conn conn_limit_per_ip 35;
        limit_req zone=req_limit_per_ip burst=35;

        include fastcgi_params;
        fastcgi_param HTTPS on;
        fastcgi_pass 127.0.0.1:9000;
    }

    location ~* \.(png|jpg|dst|xls) {
        try_files $uri $uri/ @nofile;
    }
    location @nofile {
        rewrite ^ /images/404.png;
    }

    if ($uri ~* ^/([a-zA-Z0-9_\-]+)$) {
        rewrite ^/([a-zA-Z0-9_\-]+) /vanity_url.php?mask=$1&$args;
    }

    location /v1 {
        proxy_pass http://api.commonskudev.com:8080;
        proxy_set_header Host $host;
    }
}

server {
    listen 80 default_server;
    server_name rightsleeve.commonskudev.com;
    rewrite ^(.*) https://$host$1 permanent;
}

1 个答案:

答案 0 :(得分:1)

您可以使用类似于此的重写:

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;
}

取自this SO回答。