簡體   English   中英

一個請求而不是另一個請求的Nginx php-fpm 404錯誤

[英]Nginx php-fpm 404 error for one request and not another

我在同一台服務器上托管多個php slim應用程序。 它們位於路徑apis/'tier'/'organization'/'appName'/'version' ,例如apis/FreeTierSmall/master/exampleApp/v1

我將Nginx與php-fpm結合使用,並且遇到了一個非常奇怪的錯誤。 我正在嘗試將以apis/master/開頭的所有請求重定向到apis/FreeTierSmall/master 我打開了Nginx rewrite_log,可以看到文件被正確重定向了。 如果嘗試apis/FreeTierSmall/master/example/v1我會得到正確的結果。 但是,如果我嘗試將apis/master/example/v1重定向到相同的php文件,則會收到404錯誤。 我知道重定向有效,因為我可以在日志中看到它。 似乎php-fpm有問題。 我在php-fpm執行中添加了標頭,所以我知道它正在調用正確的腳本。 出於某種原因,盡管在一種情況下,對同一文件的請求會產生404錯誤,而在另一種情況下卻不會。

是否有一些參數可能導致傳遞給fpm的同一文件在一個實例中起作用而在另一個實例中不起作用?

這是我的nginx配置:

worker_processes  1;
pid /run/nginx.pid;
user nginx www-data;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main_timed  '$remote_addr - $remote_user [$time_local] "$request" '
                            '$status $body_bytes_sent "$http_referer" '
                            '"$http_user_agent" "$http_x_forwarded_for" '
                            '$request_time $upstream_response_time $pipe $upstream_cache_status'
                            'FPM - $document_root - $fastcgi_script_name > $request';

    access_log /var/log/nginx/access.log main_timed;
    # error_log /dev/stderr notice;
    error_log /var/log/nginx/error.log debug;
    # error_log above can be debug
    rewrite_log on;

    keepalive_timeout  65;

    server {
        listen [::]:80 default_server;
        listen 80 default_server;
        server_name _;

        sendfile off;

        root /var/www/html;
        index index.php index.html;
        error_page 404 /404.html;

        # NOTE: Once you use last, that is the last redirect you can do. You must find the file after that.

        # HEALTH CHECK
        location /apis/FreeTierSmall/elb-status {
          access_log off;
          return 200 'A-OK!';
          # because default content-type is application/octet-stream,
          # browser will offer to "save the file"...
          # the next line allows you to see it in the browser so you can test
          add_header Content-Type text/plain;
        }

        # NORMAL API PATHS
        location /apis/ {

            #rewrite the old apis
            rewrite ^/apis/master/([\w-]+)/([\w-]+)(.*)$ /apis/FreeTierSmall/master/$1/$2/api.php$3 last;
            rewrite ^/apis/interfaceop/([\w-]+)/([\w-]+)(.*)$ /apis/FreeTierSmall/interfaceop/$1/$2/api.php$3 last;
            # add api.php to the path of the file
            rewrite ^/apis/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)(.*)$ /apis/$1/$2/$3/$4/api.php$5 last;
        }

        # ANY OTHER FILES
        location / {
            # try to serve the file, the directory, or a 404 error
            add_header X-debug-message-2 "A static file was served or 404 error $uri" always;
            try_files $uri $uri/ /robots.txt; # Need to change back to =404
        }

        # ERRORS
        # redirect server error pages to the static page /50x.html
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root /var/lib/nginx/html;
        }

        # PHP FILES
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php {
            add_header X-debug-message-5 "fastCGI -> .php $document_root$fastcgi_script_name" always;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            include fastcgi_params;
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_read_timeout 300;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        }

        # SECURITY CONCERNS
        # deny access to . files, for security
        location ~ /\. {
            log_not_found off;
            deny all;
        }
    }
}

事實證明,問題在於請求的URI不會隨重寫而更改。 Slim提供了404錯誤,因為該路由不存在,並且該路由不存在,因為URI從未通過重寫而更改。 因此,重寫實際上並不會改變請求參數,它只是用來決定要提供哪個文件的。 對於大多數用例來說,這是可以的,但對於api來說卻是可怕的。 以后遇到這個的人,祝你好運。

解決方案:使用proxy_pass。

location /apis/master/ {
    # Reroutes /apis/master/* to /apis/FreeTierSmall/master/* correctly!
    proxy_pass http://localhost:80/apis/FreeTierSmall/master/;
}

暫無
暫無

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

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