【问题标题】:Nginx 1.9.10 & Laravel 8 gives response of text/html; charset=UTF-8 but expected application/jsonNginx 1.9.10 & Laravel 8 给出 text/html 的响应; charset=UTF-8 但预期应用程序/json
【发布时间】:2021-07-30 23:08:45
【问题描述】:

我已经在云服务器上部署了 Nginx 1.9.10 和 Laravel 8,conf 文件如下:

server {
# Set the port to listen on and the server name
listen 80 default_server;

# Set the document root of the project
root /var/www/html/public;

# Set the directory index files
index index.php index.htm index.html;

# Specify the default character set
charset utf-8;

# Setup the default location configuration
location / {
    try_files $uri $uri/ /index.php?$query_string;
}

# Specify the details of favicon.ico
location = /favicon.ico { access_log off; log_not_found off; }

# Specify the details of robots.txt
location = /robots.txt  { access_log off; log_not_found off; }

# Specify the logging configuration
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

sendfile off;

client_max_body_size 100m;

# Specify what happens when PHP files are requested
location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass php:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param APPLICATION_ENV development;
    fastcgi_intercept_errors off;
    fastcgi_buffer_size 16k;
    fastcgi_buffers 4 16k;
}

# Specify what happens what .ht files are requested
location ~ /\.ht {
    deny all;
}
}

但即使我从 laravel 返回 json,api 仍将 Content-Type 作为 html 返回。

检查 .htaccess 文件,一切就绪。

Laravel:代码

Response::json([
        "success" => true,
        "message" => "Success message here",
    ]);

【问题讨论】:

  • 发布您如何在 Laravel 中返​​回 json 的 代码
  • @DanilaVershinin:我已经用示例代码更新了帖子

标签: laravel docker nginx http-headers laravel-8


【解决方案1】:

我也遇到过类似的问题,在我的一个文件中的

删除该行解决了问题。

Extra line

【讨论】:

  • 谢谢@sagar 这真的很有帮助。现在问题已经解决了。
猜你喜欢
  • 1970-01-01
  • 2011-07-12
  • 1970-01-01
  • 1970-01-01
  • 2018-11-15
  • 1970-01-01
  • 2011-06-17
  • 1970-01-01
  • 2020-10-26
相关资源
最近更新 更多