Issue
I am currently dockerizing an API developped in Laravel and I am using Nginx as a reverse_proxy, here is the conf.d file
server {
listen 80;
index index.php index.html;
root /var/www/public;
client_max_body_size 32M;
fastcgi_intercept_errors on;
proxy_intercept_errors on;
location / {
try_files $uri /index.php$is_args$args;
}
location ~ \.php$ {
add_header Access-Control-Allow-Origin http://foo.bar;
add_header Access-Control-Max-Age 3600;
add_header Access-Control-Expose-Headers Content-Length;
add_header Access-Control-Allow-Headers Range;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass laravel:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
internal;
}
I am getting the Internal server response bodies and 404 Not Found responses but the problem is that the HTTP Status Code is always 200. I need the HTTP Status Code to be sent as it is to the client making the request because I am using 401, 403 and others for the web app client application, like this
if ($isAuthenticated) {
return $token
} else {
return response()->json(['error'='Unauthorized','attempts'=$number ],401);
}
Then the 401 Error is catched by Ajax to show a specific alert to the user. And Nginx is getting in the way of doing that with sending back always 200. I tried setting fastcgi_intercept_errors and proxy_intercept_errors on, but it still does not work. The image i am using is nginx:1.19.8-perl and I tried the nginx:1.17-alpine one. Is there any way I can do this ? Or probably I am using Nginx for the wrong purpose and there is a better reverse proxy for APIs ?
Solution
I figured it out. The problem was not with nginx but with the PHP-FPM container, i missed to command the container to use the php.ini-production file as the php.ini configuration file. Adding this command in the Dockerfile fixed the issue and HTTP Errors are being sent back through the nginx reverse proxy the way I want it.
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
Answered By - joe1531 Answer Checked By - Timothy Miller (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.