【问题标题】:All routes return 404 when deploying on server laravel+nuxt+nginx在服务器 laravel+nuxt+nginx 上部署时所有路由都返回 404
【发布时间】:2020-09-08 06:03:51
【问题描述】:

我正在尝试将 both Laravel 6.2 作为后端和 Nuxt 2.11 作为前端(通用模式)上传到服务器,但上传后每个路由返回 laravel 404,我也在使用 nginx 反向代理(生产模式)

使用这个 laravel-nuxt 包https://github.com/cretueusebiu/laravel-nuxt

上传步骤

1- 上传后端文件和文件夹(app,bootstrap,client,config,database,routes,storage,vendor,package.json and lock,composer.json and lock)

2-上传前端文件和文件夹(.nuxt,以及我的客户端文件夹中的所有内容)

3-在我的服务器中,我在 nginx.conf 的末尾添加了一个新行 include /etc/nginx/sites-enabled/*.conf;

4- 然后在 /etc/nginx/sites-enabled 我有 default.conf 包含以下内容

server {
  # server on port 80 (default http port)
  listen 80;
  server_name rabter.com;

  # proxy for frontend
  location / {
    # nuxt server url
    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;
    proxy_cache_bypass $http_upgrade;
  }

  # proxy for api
  location /api/* {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    # laravel server url
    proxy_pass http://localhost:8000;
    proxy_redirect off;
  }
}

启用我的服务器块并重新启动 Nginx ln -s /etc/nginx/sites-available/default.conf /etc/nginx/sites-enabled/

5- 我设置了 pm2 服务器,所以我运行 pm2 start Laravel-nuxt 开始监听端口 3000(午餐 nuxt)

此时我访问我的网站,该网站在所有路线上都返回 404,无一例外 我也遇到过这个https://github.com/iliyaZelenko/laravel-nuxt/issues/1#issuecomment-491484474 我认为这基本上是我想做的,甚至尝试了 nginx 代码但没有用

我的 web.php 中有 0 条路由,所有路由都在 api.php 中。此外,这个项目在本地主机上运行良好,无论是开发模式还是产品模式,但是当我移动到服务器时,它都是 404。

【问题讨论】:

    标签: node.js laravel nginx nuxt.js laravel-6.2


    【解决方案1】:

    正确的 ssl 配置文件 http配置文件

    server {
      listen your-server-ip:80;
        server_name example.com;
            return 301 https://www.example.com$request_uri;
    
    }
    

    ssl配置文件

    server {
      listen your-server-ip:443 ssl;
        server_name example.com;
            return 301 https://www.example.com$request_uri;
    
    }
    server {
        listen your-server-ip:443 ssl;
        server_name www.example.com;
        ssl_certificate /etc/pki/tls/certs/example.com.bundle;
        ssl_certificate_key /etc/pki/tls/private/example.com.key;
          root /home/example/core/public/;
            index index.php;
            access_log /var/log/nginx/example.com.bytes bytes;
           access_log /var/log/nginx/example.com.log combined;
          error_log /var/log/nginx/example.com.error.log error;
    
    location / {
    
        proxy_set_header                Connection "keep-alive";
        proxy_set_header                Upgrade $http_upgrade;
        proxy_set_header                Connection 'upgrade';
        proxy_http_version              1.1;
        proxy_pass                      https://your-server-ip:3000$uri;
        proxy_intercept_errors          on;# In order to use error_page directive this needs to be on
        error_page                      404 = @php;
    }
    
    location @php {
        try_files                       $uri $uri/  /index.php?$query_string;
    
    }
    
    location ~ \.php$ {
       fastcgi_split_path_info         ^(.+\.php)(/.+)$;
        fastcgi_pass                    your-server-ip:9000;
        fastcgi_index                   index.php;
        include                         fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       fastcgi_intercept_errors        off;
        fastcgi_buffer_size             16k;
        fastcgi_buffers                 4 16k;
        fastcgi_connect_timeout         300;
       fastcgi_send_timeout            300;
        fastcgi_read_timeout            300;
    }
    }
    
    

    请记住,我的端口是3000,您的端口可能不同

    fastcgi_pass 也可以指向袜子,但我直接添加了它

    fastcgi_pass 端口在我的情况下设置为 9000 你的可以不同

    话虽如此,我们将 80 和 433 不带 www 重定向到 www 然后做一个反向代理,如果反向代理是 404 我们尝试@php 和在底部我们使用 php-fpm 来运行我们的 php 代码

    我几乎花了 2 周的时间学习 nginx 并编写了不同的配置,直到我想出了这个

    【讨论】:

      猜你喜欢
      • 2020-08-26
      • 1970-01-01
      • 1970-01-01
      • 2017-08-23
      • 2022-12-01
      • 2021-01-06
      • 2014-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多