php - 使用 nginx 在 linux 服务器上部署 Laravel 应用程序出现 404 错误

标签 php linux laravel nginx deployment

这是我类(class)期末项目的最后一步。我按照 Digital-Ocean {{ https://www.digitalocean.com/community/tutorials/how-to-deploy-a-laravel-application-with-nginx-on-ubuntu-16-04 }} 但我在调用 URL 时遇到了 404 错误。

以下代码是nginx的配置。由于我是 laravel 的新手,我也不知道如何部署它。这也是我第一次使用服务器进行部署。

    # Default server configuration
    #
    server {
            listen 80;
            listen [::]:80;
 # SSL configuration
        #
         #listen 443 ssl http2;
        # listen [::]:443 ssl http2;

        access_log            /var/log/nginx/jenkins.access.log;
        error_log            /var/log/nginx/jenkins.error.log;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
 # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/ssl-marikhu.com.conf;
        #include snippest/ssl-params.conf;

        root /var/www/laravel/smartroom/public;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.ngix-debian.html;

        server_name marikhu.com www.marikhu.com;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
  try_files $uri $uri//index.php?query_string;
                include /etc/nginx/proxy_params;
                proxy_pass          http://localhost:8080;
                proxy_read_timeout  90s;
                # Fix potential "It appears that your reverse proxy set up is b$
                proxy_redirect      http://localhost:8080 https://marikhu.com;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
      location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        #
        #       # With php7.0-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php7.0-fpm:
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

        # deny access to .htaccess files, if Apache's document root
  # concurs with nginx's one
        #
        location ~ /\.ht {
                deny all;
        }

    listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/marikhu.com/fullchain.pem; # managed by C$
ssl_certificate_key /etc/letsencrypt/live/marikhu.com/privkey.pem; # managed by$
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#       listen 80;
#       listen [::]:80;
#
#       server_name moodymountains.marikhu.com;
#
#       root /var/www/html;
#       index index.php;
#
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#       listen 80;
#       listen [::]:80;
#
#       server_name moodymountains.marikhu.com;
#
#       root /var/www/html;
#       index index.php;
#
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#       listen 80;
#       listen [::]:80;
#
#       server_name moodymountains.marikhu.com;
#
#       root /var/www/html;
#       index index.php;
#}
#       location / {
#               try_files $uri $uri/ =404;
#       }
#}

最佳答案

替换这个 block

location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
  try_files $uri $uri//index.php?query_string;
                include /etc/nginx/proxy_params;
                proxy_pass          http://localhost:8080;
                proxy_read_timeout  90s;
                # Fix potential "It appears that your reverse proxy set up is b$
                proxy_redirect      http://localhost:8080 https://marikhu.com;
        }

用这个:

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

保存文件,重启nginx服务器,再次尝试访问。

关于php - 使用 nginx 在 linux 服务器上部署 Laravel 应用程序出现 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47732264/

相关文章:

php - 如何使用 eloquent with 访问 laravel 中的表不直接附加

javascript - 将 onhover 更改为 onclick

python - 为什么我的 Python 脚本在我的 Raspberry Pi 启动时通过将命令添加到/etc/profile 来尝试在后台运行两次?

php - SQL不按php查询降序排序

linux - 尾部-f + grep?

linux - 如果命令超过 1 行,如何在 shell 或 bash 中输入新行/移动到下一行而不执行命令?

php - Composer 安装在 Laravel 5 上崩溃(仅限生产环境)

php - Laravel 使用查询字符串构建外部 URL 的最佳方法是什么?

javascript - 显示循环中的所有数据

php - 是否可以在 PHP 中重载构造函数