簡體   English   中英

如何讓我的 Laravel 應用程序在 Nginx 服務器上運行?

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

我剛剛使用帶有 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; } }

我在我的服務器上使用它。 測試一下:

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM