繁体   English   中英

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

[英]How to remove .php extensions in nginx config?

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

下面是我在亚马逊远程服务器上的 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;
}

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

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

这个 SO答案中得出。

用这个:

server {
  listen 80;
  server_name www.example.local;
  root /var/www/vhosts/example/httpdocs;

  index index.html index.htm index.php;

  location / {
    try_files $uri $uri/ @ext;
  }

  location ~ \/\.php {
    rewrite "^(.*)\/.php" $1.php last;
  }

  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
  }

  location @ext {
    rewrite "^(.*)$" $1.php;
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM