REQUEST_URI htaccess nginx

REQUEST_URI htaccess nginx

Hilfe zur Konvertierung von htaccess in nginx

RewriteEngine On
RewriteCond %{REQUEST_URI} apiv01
RewriteRule ^(.*)$ api.php?params=$1 [NC]

Ich möchte, wenn Zugriff uri GEThttp://localhost/hospital_project/apiv01/listHospital/3211kann anzeigen

    {"header":{"code":"401","message":"wrong token"}}

Ich habe die Konfiguration von Anilcetin wie folgt verwendet:

location /hospital_project {
if ($uri ~ "apiv01"){
        set $rule_0 1$rule_0;
        }
if ($rule_0 = "1"){
        rewrite ^/(.*)$ /hospital_project/api.php?params=$1;
        }

}

Außerdem habe ich die Konfiguration vonhttps://winginx.com/en/htaccessso was:

location /hospital_project {
  rewrite apiv01 /hospital_project/api.php?params=$1;
}

Aber wenn ich zugreifehttp://localhost/hospital_project/apiv01/listHospital/3211Die Ausgabe ist gleichhttp://localhost/hospital_project/apiv01(Kein Effekt)

Normal, wenn ich benutzehttp://localhost/hospital_project/api.php?params=apiv01/listHospital/3211

--- Bearbeiten, ich benutze auch

location /hospital_project/apiv01 { try_files $uri $uri/ /hospital_project/api.php?params=$request_uri; } aber auch keine Wirkung

Dies wird durch die Verwendung eines Reverse-Proxys für Apache2 wie folgt gelöst

location /hospital_project {

proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8888;
}

aber ich bin immer noch neugierig, ob die Verwendung von Rewrite in Nginx

Antwort1

nginx implementiert Front-Controller-Muster einfacher und effektiver als Apache. Die nginx-Methode sieht in Ihrem Fall folgendermaßen aus:

location ~ ^/(hospital_project/apiv01.+)$ {
    try_files $uri $uri/ /hospital_project/api.php?params=$1;
}

Sie fügen diesen Speicherort lediglich oben in der Nginx-Konfiguration hinzu. Sie müssen überhaupt keine ifoder -Direktiven verwenden.rewrite

Antwort2

Entschuldigung, ich meine, ich verwende;

location /hospital_project/ {
  rewrite apiv01 /hospital_project/api.php?params=$1;
}

aber keine Wirkung

auch ich benutze

location /hospital_project/apiv01 {
    try_files $uri $uri/ /hospital_project/api.php?params=$request_uri;
}

aber auch keine Wirkung

Dies wird durch die Verwendung eines Reverse-Proxys für Apache2 wie folgt gelöst

    location /hospital_project {

    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://127.0.0.1:8888;
    }

aber ich bin immer noch neugierig, ob die Verwendung von Rewrite in Nginx

verwandte Informationen