简体   繁体   中英

Nginx from .htaccess PHP8 Laravel

So I have been trying to find a solution to this over the last few days but can't seem to get it to work.

I have a laravel programme which used to run on AWS but now migrating to Azure Web Apps. I keep getting the following error: '404 Not Found. nginx/1.14.' I have a .htaccess file as follows:

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

I understand that .htaccess doesn't work with php 8 so i created a nginx.config file in the root directory with the following code:

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


}

I still can't get it to work. Any help would be appreciated.

.htaccess works fine with PHP8, as the two are not related. .htaccess is an Apache config thing: you've done the right thing by ditching it and going to the config.

The bit you need to replicate in your config is

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

You should be able to do that with

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

.htaccess doesn't work with php 8

No, .htaccess does not work with nginx, it is an apache config file

Probably you need to set correctly the site root.

root /var/www/html;

Assuming that your project reside inside /var/www/html directory, for laravel project you should point to /var/www/html/public in your webserver configuration.

Look in your old apache configuration the DocumentRoot setting, it should point to public as well

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM