首页 > 解决方案 > 如何让我的 laravel 应用程序在 Nginx 服务器上运行?

问题描述

我刚刚使用带有 Nginx 服务器的 LEMP 堆栈在 DigitalOcean 上部署了我的 Laravel 应用程序。

但是,当我导航到我的服务器 IP 地址时,我收到 403 Forbidden 错误。然后当我转到 xxx.xx.xxx.xx/public 时,即使我已经将我的根目录设置为我的公用文件夹,我也可以正确看到我的主页。此外,当我单击主页上的任何链接时,它会给我一个 404 Not Found。

这是我的 etc/nginx/sites-available/default:

我究竟做错了什么?请不要将我的问题标记为重复,因为我看到大多数类似的主题他们忘记将 Index.php 添加到他们的索引列表或忘记将他们的根链接到他们的公用文件夹,但是我做的都是正确的。

我的配置文件:

# Default server configuration 
#
server {
    listen 80 default_server;
    listen [::]:80 default_server; 

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # 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/snakeoil.conf; 

    root /var/www/html/public/; 

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

    server_name 159.65.192.29; 

    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; 
    } 

    # pass PHP scripts to FastCGI server
    #
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
    #
    #   # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    #   # With php-cgi (or other tcp sockets):
    #   fastcgi_pass 127.0.0.1:9000; 
    }
}

标签: phplaravelnginxserver

解决方案


尝试将您的server.php名称更改index.php为项目的根目录,并创建一个nginx.conf包含以下内容的文件:

# nginx configuration location ~* (\.css|\.mp4|\.woff|\.js|\.png|\.jpg|\.gif|robots\.txt)$ { } location ~ /public/ { } location / { if (!-e $request_filename){ rewrite ^/(.*)/$ /$1 redirect; } if (!-e $request_filename){ rewrite ^(.*)$ /index.php break; } if (!-e $request_filename){ rewrite ^/(css|js|images|favicon|fonts|videos|storage)/(.*)$ /public/$1/$2 break; } }

推荐阅读