错误“请求失败,状态码为404”

时间:2019-05-27 09:41:22

标签: json nginx https axios nuxt

我正在NGINX + MYSQL + PHP Ubuntu 18.04服务器上运行“通用” Nuxt项目。有些页面使用Axios从数据库(PHP创建的JSON文件)中获取数据。该项目在开发和生产模式上运行良好。使用nginx作为反向代理的服务器(localhost:3000-> localhost:80)。

但是在我安装HTTPS和SSL证书(DigitalOcean手册:如何在Ubuntu 18.04上使用Let's Encrypt保护Nginx)之后,服务器开始在生产模式下显示错误:

 ERROR  Request failed with status code 404                                                                

  at createError (node_modules/axios/lib/core/createError.js:16:15)
  at settle (node_modules/axios/lib/core/settle.js:18:12)
  at IncomingMessage.handleStreamEnd (node_modules/axios/lib/adapters/http.js:201:11)
  at IncomingMessage.emit (events.js:194:15)
  at IncomingMessage.EventEmitter.emit (domain.js:441:20)
  at endReadableNT (_stream_readable.js:1125:12)
  at process._tickCallback (internal/process/next_tick.js:63:19)

我尝试了来自Nuxt官方站点的nginx配置示例。但是出现错误保持。

我的配置文件/etc/nginx/site-available/web_site.com

map $sent_http_content_type $expires {
    "text/html"                 epoch;
    "text/html; charset=utf-8"  epoch;
    default                     off;
}

server {
    root /var/www/html;
    server_name web_site.com www.web_site.com;
    gzip            on;
    gzip_types      text/plain application/xml text/css application/javascript;
    gzip_min_length 1000;
    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/web_site.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/web_site.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    }

    location /basemysql {
    auth_basic "Admin Login";
        auth_basic_user_file /etc/nginx/pma_pass;
    }

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    }
}

server {
    if ($host = www.web_site.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = web_site.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;
        server_name web_site.com www.web_site.com;
    return 404; # managed by Certbot

}

该应用程序将完全正常运行,直到您尝试重新加载它为止。每次我尝试重新加载具有Axios的任何页面时都会出现错误。

1 个答案:

答案 0 :(得分:0)

我发现了问题。从HTTP重定向到HTTPS导致错误。 我删除了这些配置,并且工作正常。

server {
    if ($host = www.web_site.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = web_site.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;
        server_name web_site.com www.web_site.com;
    return 404; # managed by Certbot

}