简体   繁体   中英

How do I make my laravel app work on a Nginx server?

I just deployed my Laravel application on DigitalOcean using the LEMP stack with a Nginx server.

However when I navigate to my servers IP adress, I get a 403 Forbidden error. Then when I go to xxx.xx.xxx.xx/public, I can see my homepage correctly even though I set my root to my public folder already. Also, when I click on any link on my homepage, it gives me a 404 Not Found.

This is my etc/nginx/sites-available/default:

What am I doing wrong? Please don't mark my question as a duplicate, because most of similar topics I see they forget to add Index.php to their index list or forget to link their root to their public folder, however I'm doing both correctly.

My config file:

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

I use this on my server. test it:

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

}

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