php - 如何让我的 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;
}
}
解决方案
尝试将您的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; } }
推荐阅读
- excel - 隐藏多个工作表并仅显示一个工作表 vba
- node.js - 安装依赖项以处理现有的 Angular 项目
- java - 如何在内部或从正在运行的应用程序中的依赖项执行 jar?
- python - 如何使用 ctypes 将 SendInput 函数包装到 python
- c++ - 结构中带有空格的字符串
- ffmpeg - FFmpeg 过滤器在特定点将音频文件添加到视频中
- apache-spark - 通过 Spark 将文件附加到现有的 S3 存储桶文件夹
- algorithm - 使用 mod 操作时如何计算时间复杂度
- swift - 子视图的意外帧(Swift)
- angular - .Net Core WebApi + Angular 项目中的安全 SPA