使用nginx在Linux服务器上部署Laravel app得到404错误

时间:2017-12-09 19:23:32

标签: php linux laravel nginx deployment

这是我班级最终项目的最后一步。我按照Digital-Ocean {{https://www.digitalocean.com/community/tutorials/how-to-deploy-a-laravel-application-with-nginx-on-ubuntu-16-04}}中的步骤操作,但在调用URL时出现404错误。

以下代码是nginx配置。由于我是laravel的新手,我也不知道如何部署它。这是我第一次使用服务器进行部署。

    # Default server configuration
    #
    server {
            listen 80;
            listen [::]:80;
 # SSL configuration
        #
         #listen 443 ssl http2;
        # listen [::]:443 ssl http2;

        access_log            /var/log/nginx/jenkins.access.log;
        error_log            /var/log/nginx/jenkins.error.log;
        #
        # 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/ssl-marikhu.com.conf;
        #include snippest/ssl-params.conf;

        root /var/www/laravel/smartroom/public;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.ngix-debian.html;

        server_name marikhu.com www.marikhu.com;

        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;
                include /etc/nginx/proxy_params;
                proxy_pass          http://localhost:8080;
                proxy_read_timeout  90s;
                # Fix potential "It appears that your reverse proxy set up is b$
                proxy_redirect      http://localhost:8080 https://marikhu.com;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
      location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        #
        #       # With php7.0-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php7.0-fpm:
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

        # deny access to .htaccess files, if Apache's document root
  # concurs with nginx's one
        #
        location ~ /\.ht {
                deny all;
        }

    listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/marikhu.com/fullchain.pem; # managed by C$
ssl_certificate_key /etc/letsencrypt/live/marikhu.com/privkey.pem; # managed by$
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#       listen 80;
#       listen [::]:80;
#
#       server_name moodymountains.marikhu.com;
#
#       root /var/www/html;
#       index index.php;
#
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#       listen 80;
#       listen [::]:80;
#
#       server_name moodymountains.marikhu.com;
#
#       root /var/www/html;
#       index index.php;
#
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#       listen 80;
#       listen [::]:80;
#
#       server_name moodymountains.marikhu.com;
#
#       root /var/www/html;
#       index index.php;
#}
#       location / {
#               try_files $uri $uri/ =404;
#       }
#}

1 个答案:

答案 0 :(得分:0)

替换此块

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;
                include /etc/nginx/proxy_params;
                proxy_pass          http://localhost:8080;
                proxy_read_timeout  90s;
                # Fix potential "It appears that your reverse proxy set up is b$
                proxy_redirect      http://localhost:8080 https://marikhu.com;
        }

用这个:

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

保存文件,重新启动nginx服务器并尝试再次访问。