You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx配置Laravel 5.4站点:子路径访问403错误求助

Fixing Laravel 5.4 Deployment Issues on EC2 Ubuntu 16.04 with Nginx 1.10.3

Let's break down your problems one by one and fix them step by step:

The core issue here is your misconfigured Nginx server blocks — you can't include a path in server_name, and we need to set up a sublocation to handle the /my-site route correctly.

Step 1: Update the Default Nginx Site Config

Edit /etc/nginx/sites-available/default with this revised configuration:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name xxx.xxx.xxx.xxx;

    location / {
        try_files $uri $uri/ =404;
    }

    # Add this block to handle your Laravel app at /my-site
    location /my-site {
        alias /var/www/html/my-site/public;
        try_files $uri $uri/ /my-site/index.php?$query_string;

        # Handle PHP files for the subpath
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            # Critical: Fix script path for alias setup
            fastcgi_param SCRIPT_FILENAME $request_filename;
        }
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

Step 2: Disable the Misconfigured my-site Server Block

Your separate my-site config uses an invalid server_name (you can't add a path here), so we'll remove its symlink and clean it up:

sudo unlink /etc/nginx/sites-enabled/my-site
# Optional: Delete the config file if you don't need it later
sudo rm /etc/nginx/sites-available/my-site

Update your Laravel .env file to point to the correct subpath:

cd /var/www/html/my-site
nano .env

Modify the APP_URL line:

APP_URL=http://xxx.xxx.xxx.xxx/my-site

Clear Laravel's cached config to apply the change:

php artisan config:clear
php artisan cache:clear

2. Fix "Connection Refused" for php artisan serve

This is almost always a firewall/security group issue:

Step 1: Open Port 8000 in EC2 Security Group

  1. Go to your EC2 Dashboard → Instances → Select your server → Security tab
  2. Click "Security groups" → Edit inbound rules
  3. Add a new rule:
    • Type: Custom TCP
    • Port range: 8000
    • Source: 0.0.0.0/0 (or your specific IP for tighter security)

Step 2: Allow Port 8000 in UFW (if enabled)

If you're using Ubuntu's UFW firewall, run:

sudo ufw allow 8000/tcp
sudo ufw reload

Final Checks

Restart Nginx and PHP-FPM to apply all changes:

sudo systemctl restart nginx
sudo systemctl restart php7.0-fpm

Now you should be able to:

  • Access your Laravel app at http://xxx.xxx.xxx.xxx/my-site without 403 errors
  • Use all links correctly (no broken paths)
  • Reach php artisan serve at http://xxx.xxx.xxx.xxx:8000

内容的提问来源于stack exchange,提问作者Afrog Nuñes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:39:13