Nginx配置Laravel 5.4站点:子路径访问403错误求助
Let's break down your problems one by one and fix them step by step:
1. Resolve 403 Error for /my-site & Broken Links on /my-site/public
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
Step 3: Fix Laravel's Asset & Route Links
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
- Go to your EC2 Dashboard → Instances → Select your server → Security tab
- Click "Security groups" → Edit inbound rules
- 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-sitewithout 403 errors - Use all links correctly (no broken paths)
- Reach
php artisan serveathttp://xxx.xxx.xxx.xxx:8000
内容的提问来源于stack exchange,提问作者Afrog Nuñes

