【问题标题】:How do I make my laravel app work on a Nginx server?如何让我的 laravel 应用程序在 Nginx 服务器上运行?
【发布时间】:2019-04-12 12:06:47
【问题描述】:

我刚刚使用带有 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; 
    }
}

【问题讨论】:

  • 你能用你的 nginx 配置作为代码而不是截图来更新你的帖子吗?
  • 由于某种原因,我无法在我的 Ubuntu 环境中复制/粘贴。
  • 你的整个 Laravel 项目都在/var/www/html/吗?
  • 你记得编辑配置后重新加载 nginx 吗?
  • 我用代码块更新了我的问题。是的,我的项目位于 /var/www/html/。

标签: php laravel nginx server


【解决方案1】:

尝试在项目的根目录中将您的 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; } }

【讨论】:

  • 我重命名了文件,现在它可以从我的索引页面正确显示。你能解释一下为什么我必须将它更改为 index.php 而我已经告诉服务器我的根目录在 /public 中吗?
【解决方案2】:

我在我的服务器上使用它。测试一下:

server {
        root         /var/www/html/public;
        server_name [your_ip_address];
        index index.php;
        # Load configuration files for the default server block.
        location / {
                try_files $uri $uri/ /index.php?$query_string;
        }

## Laravel Production
        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-XSS-Protection "1; mode=block";
        add_header X-Content-Type-Options "nosniff";
        charset utf-8;
        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }
        location ~ /\.(?!well-known).* {
                deny all;
        }

        location ~ \.php$ {
                try_files $uri /index.php =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }

}

【讨论】:

  • 谢谢,但我已经解决了。我正在使用 /sites-available/default 而我需要我的托管 /sites-available/digitalocean 之一。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-27
  • 2020-01-10
  • 1970-01-01
相关资源
最近更新 更多