簡體   English   中英

php nginx 將 URL 重寫為 index.php 與

[英]php nginx rewrite urls to index.php with

我一直試圖讓它工作一段時間,但我失敗了很多。

我有以下配置:

server {
        listen 8081;
        server_name name.of.server.en;
        root /path/to/api;
        index index.php;

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

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

        location @rewrite {
                rewrite ^/([A-Za-z0-9]+)/$ /index.php?data=$1? last;
                rewrite ^/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$ /index.php?data=$1&id=$2? last;
                return 404;
        }
}

nginx -t表示一切正常。 但是一旦我打電話給 URL 我總是得到一個404 Not Found

我不知道我做錯了什么。 可能是完全平庸的東西,但我無法弄清楚。 我幾乎絕望了。

這是我為 PHP7.4 運行的本地測試。 可能對你有幫助。

server {

    listen 80;
    root /var/www/html/public;
    index index.html index.php;
    server_name fancyproject;

    charset utf-8;

    location / {
        root /var/www/html/vue/dist;
        index index.html;
        try_files $uri $uri/ /index.html;
    }

    location /api/ {
        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; }

    add_header "Access-Control-Allow-Origin"  *;
    add_header "Access-Control-Allow-Methods" "GET,POST,OPTIONS,HEAD,PUT,DELETE,PATCH";
    add_header "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept, X-Nonce, DNT, X-CustomHeader";

    access_log off;
    error_log /var/log/nginx/error.log error;

    sendfile off;

    client_max_body_size 100m;

    location ~ .php$ {
        fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
    }

    location ~ /.ht {
        deny all;
    }
}

在這一點上,順序很重要。 這是它應該如何工作的:

server {
        listen 8080;
        server_name the.server.name.org;
        root /path/to/api;
        index index.php;

        location / {
                rewrite ^/(.*)$ /index.php?data=$1 last;
        }

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM