如何使用Nginx和PHP后端修复重定向到尾部的斜杠

时间:2019-05-29 15:02:12

标签: nginx fastcgi nginx-location php-7.2

就像在这个thread中一样,我在斜杠后面也遇到了同样的问题。

目录/ var / www / services包含多个后端服务。 例如,我请求GET /customer?arg1=1&arg2=2,nginx将始终将我重定向到/customer/?arg1=1&arg2=2

Nginx日志输出:

172.18.0.6 - - [29/May/2019:14:47:32 +0000] "GET /customer?limit=1&filter%5Bemail%5D=somemail%40gmail.com&filter%5BwebsiteId%5D=1 HTTP/1.1" 301 170 "-" "GuzzleHttp/6.3.3 curl/7.29.0 PHP/7.0.33"
172.18.0.6 - - [29/May/2019:14:47:32 +0000] "GET /customer/?limit=1&filter%5Bemail%5D=somemail%40gmail.com&filter%5BwebsiteId%5D=1 HTTP/1.1" 200 12 "-" "GuzzleHttp/6.3.3 curl/7.29.0 PHP/7.0.33"

所有其他请求,例如customer/count都可以正常工作。

如何避免这种不良行为?

配置如下:

server {
        listen 80;
        listen 443 ssl;
        server_name api.local;
        root /var/www/services;

        ssl_certificate  /etc/ssl/certs/optimax.crt;
        ssl_certificate_key /etc/ssl/certs/optimax.key;
        ssl_dhparam /etc/ssl/certs/optimax.pem;

        location ~ ^/(?<ms_dir>[^/]+)/(.*)$ {
                root /var/www/services/$ms_dir/public;
                set $ms_request $2;
                try_files $uri /index.php$is_args$args;
        }

        location ~ /index\.php(/|$) {
                fastcgi_pass php72:9001;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                include fastcgi_params;
                fastcgi_param REQUEST_URI $ms_request;
                fastcgi_param SCRIPT_FILENAME $realpath_root/$ms_dir/public$fastcgi_script_name;
                fastcgi_param DOCUMENT_ROOT $realpath_root/$ms_dir/public;
        }

        access_log /dev/stdout;
        error_log /dev/stderr;
        rewrite_log on;
}

1 个答案:

答案 0 :(得分:0)

Richard Smith's建议很完美。

以下更新的配置:

package handler

"net/http"
"github.com/labstack/echo"
"github.com/jinzhu/gorm"

type User struct {
    gorm.Model
    FirstName           string 
    LastName            string 
}

func SignUp(c echo.Context,db *gorm.DB) error {
    u := new(models.User)
    if err := c.Bind(u); err != nil {
      println( err)
    }

    var responder Responder
    err := validate.Struct(u)
    responder.Code ="1"
    responder.Validation =validation
    db.Create(&u)

    return c.JSON(http.StatusOK, responder)
}