Nginx from .htaccess PHP8 Laravel



所以在过去的几天里,我一直在努力寻找解决方案,但似乎无法实现。

我有一个laravel程序,它过去在AWS上运行,但现在正在迁移到Azure Web应用程序。我一直收到以下错误:404未找到。nginx/1.14.'我有一个.htaccess文件如下:

<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
<Files ~ ".env$">  
Order Allow,Deny
Deny from All
</Files>
RewriteEngine On

RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]

# RewriteCond %{HTTPS} !on
# RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

我知道.htaccess不适用于php8,所以我在根目录中创建了一个nginx.config文件,代码如下:

server {
server_name sitename.com;
root /var/www/html;

add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt  { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ .php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /.(?!well-known).* {
deny all;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/something.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/something.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

我还是不能让它工作。如有任何帮助,我们将不胜感激。

.htaccess与PHP8配合良好,因为两者不相关。htaccess是Apache的配置:您已经做了正确的事情,放弃它并转到配置。

您需要在配置中复制的部分是

# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

你应该能够用做到这一点

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

.htaccess不适用于php 8

不,.htaccess不适用于nginx,它是一个apache配置文件

可能您需要正确设置站点根目录。

root /var/www/html;

假设您的项目位于/var/www/html目录中,对于laravel项目,您应该在Web服务器配置中指向/var/www/html/public。

在旧的apache配置中查看DocumentRoot设置,它应该指向public以及

最新更新