Rumah  >  Soal Jawab  >  teks badan

Menulis semula NGINX menggunakan arahan penghantaran proksi tidak berfungsi dengan betul

Saya sedang membangunkan API lama yang dijalankan pada versi Symfony yang lebih lama (2.8 sedang dinaik taraf kepada 3.4).

Aplikasi mempunyai dua titik masuk PHP "utama": app_dev.phpapp.php_dev Titik akhir ini hanya boleh digunakan dalam persekitaran pembangunan, tetapi digunakan secara salah dalam aplikasi pengeluaran yang lebih lama.

Kami boleh mengubah suai API dengan mudah untuk mengelak daripada mendedahkan fail ini (dan telah berjaya melakukannya), namun, kami tidak boleh mengemas kini beberapa aplikasi yang disambungkan ke API dengan mudah. Contohnya, sesetengah apl masih cuba menyambung menggunakan https://domain/app_dev.php/the/path.

Sebagai penyelesaian sementara semasa kami berusaha membetulkan aplikasi yang menggunakan API, kami ingin menulis semula https://domain/app_dev.php/the/path 重写为 https://domain/app.php/the/path 或者更好的是 https://domain/the/path ke https://domain/app.php/the/path atau lebih baik lagi < code>https ://domain/the/path. Kami percaya menulis semula adalah pilihan yang tepat, tetapi mungkin kami tidak betul?

Kami mempunyai konfigurasi NGINX semasa:

## nginx.conf
user                    www-data;
worker_processes        auto;
worker_rlimit_nofile    65535;
error_log               /var/log/nginx/error.log warn;
pid                     /var/run/nginx.pid;

events {
    worker_connections          4096;
}

http {
    server_tokens           off;

    include                 /etc/nginx/mime.types;
    default_type            application/octet-stream;

    # log_format must come before access_log!
    log_format              main '$remote_addr - $remote_user [$time_local] "$request" $status '
                                 '$body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"';
    access_log              /var/log/nginx/access.log  main;

    sendfile                    on;
    keepalive_timeout           65;
    client_max_body_size        50M;
    proxy_buffer_size           128k;
    proxy_buffers               4 256k;
    proxy_busy_buffers_size     256k;
    large_client_header_buffers 4 16k;

    include /etc/nginx/conf.d/*.conf;
}
## php-prod.conf
upstream php {
    server 127.0.0.1:9000;
}

server {
    listen                      80;

    client_max_body_size        50M;
    client_body_buffer_size     128k;
    fastcgi_param               REMOTE_USER $remote_user;

    root                        /var/www/web;

    location = /favicon.ico {
        break;
    }

    location / {
        try_files               $uri /app.php$is_args$args;
    }

    # Configuration for PHP passthrough on remote / production endpoints
    location ~ ^/app\.php(/|$) {
        if ($request_method = OPTIONS ) {
            add_header Content-Length 0;
            add_header Content-Type text/plain;
            add_header Access-Control-Allow-Headers "x-custom-auth, content-type, x-requested-with, authorization, mobile";
            add_header Access-Control-Allow-Methods "POST, PUT, PATCH, GET, DELETE";
            add_header Access-Control-Allow-Origin "$http_origin";
            add_header Access-Control-Max-Age 1728000;
            return 204;
        }

        fastcgi_pass php;
        fastcgi_read_timeout 3600;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;

        # When you are using symlinks to link the document root to the current version of your application, you
        # should pass the real application path instead of the path to the symlink to PHP FPM. Otherwise, PHP's OPcache
        # may not properly detect changes to your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
        # for more information).
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;

        # Prevents URIs that include the front controller. This will 404: http://domain.tld/app.php/some-path
        # Remove the internal directive to allow URIs like this
        internal;
    }

    # Return 404 for all other php files not matching the front controller.
    # This prevents access to other php files you don't want to be accessible.
    location ~ \.php$ {
        return 404;
    }

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

Saya cuba menambah rewrite /app_dev.php/(.*) /app.php break; 添加到各个地方,包括主 server 块和 location / 块和似乎无法使重定向正常工作。我尝试过使用 breaklast tetapi saya hanya menerima 500 respons daripada API tanpa kandungan.

Jika saya mengemas kini location ~ ^/app.php(/|$) {location ~ ^/(app|app_dev).php(/|$) { ia berfungsi dengan baik, jadi saya tahu API "berfungsi" di sebalik NGINX.

Saya harus menyatakan bahawa 301 ubah hala tidak akan berfungsi di sini kerana saya juga perlu menulis semula permintaan POST. Walau bagaimanapun, saya telah menguji ini sebagai ubah hala 301 dan mendapat hasil yang diharapkan (tetapi 301 tidak berfungsi untuk permintaan POST kami):

location / {
        rewrite /app_dev\.php/(.*) / permanent;
        try_files               $uri /app.php$is_args$args;
    }

...

127.0.0.1 - testCommand [25/May/2023:09:04:31 +0000] "POST /app_dev.php/api/path HTTP/1.0" 301 162 "-" "PostmanRuntime/7.32.2"
172.20.0.1 - testCommand [25/May/2023:09:04:31 +0000] "POST /app_dev.php/api/path HTTP/1.1" 301 162 "-" "PostmanRuntime/7.32.2" "-"
172.20.0.1 - - [25/May/2023:09:04:31 +0000] "GET /api/path HTTP/1.1" 301 162 "https://api.domain.com/app_dev.php/api/path" "PostmanRuntime/7.32.2"

Jika saya menetapkannya untuk mengganggu:

172.20.0.1 - testCommand [25/May/2023:09:08:05 +0000] "POST /app_dev.php/api/path HTTP/1.1" 500 5 "-" "PostmanRuntime/7.32.2" "-"
127.0.0.1 - testCommand [25/May/2023:09:08:05 +0000] "POST /app_dev.php/api/path HTTP/1.0" 500 0 "-" "PostmanRuntime/7.32.2"

Jika saya tetapkan untuk bertahan:

172.20.0.1 - testCommand [25/May/2023:09:13:57 +0000] "POST /app_dev.php/api/path HTTP/1.1" 500 5 "-" "PostmanRuntime/7.32.2" "-"
127.0.0.1 - testCommand [25/May/2023:09:13:57 +0000] "POST /app_dev.php/api/path HTTP/1.0" 500 0 "-" "PostmanRuntime/7.32.2"

Jadi soalan saya ialah bagaimana untuk membuat penulisan semula ini berfungsi, menulis semula URL seperti https://domain/app_dev.php/the/path 等 URL 重写为 https://domain/app.php /the/path 甚至更好 https://domain/the/path ke https://domain/app.php /the/path atau lebih baik lagi https:// domain/ the/path?

P粉063862561P粉063862561257 hari yang lalu321

membalas semua(1)saya akan balas

  • P粉642920522

    P粉6429205222024-01-11 14:13:15

    Anda cuba 重写/app_dev.php/(.*) /app.php break;,但break是错误的标志 - 您需要使用<强>最后。请参阅rewriteperintah.


    Sebagai alternatif, untuk mengubah hala permintaan POST, anda boleh menggunakan 307 respons status, contohnya:

    location ~ ^/app_dev\.php(/|$) {
        return 307 /app.php$is_args$args;
    }

    Pastikan letakkan di atas blok location ~ .php$.

    Saya perasan location ~ ^/app.php(/|$) 块被标记为 internal,这会阻止您的服务器响应 的外部请求>/app.php. Ini nampaknya bercanggah dengan beberapa kenyataan dalam soalan anda.

    balas
    0
  • Batalbalas