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

部署小型网站至服务器遇403 Forbidden及字符编码未声明问题

Troubleshooting Your Nginx 403 Error & Character Encoding Issue

Hey there, let's break down these two problems you're facing after deploying your site from XAMPP to a live Nginx server. I'll walk you through fixes for each one step by step.

Fixing the 403 Forbidden nginx/1.10.3 (Ubuntu) Error

This error means Nginx can't access or serve your site files. Here's what to check:

  • Verify your Nginx root directory path
    Open your site's Nginx config file (usually in /etc/nginx/sites-available/your-site.conf) and make sure the root directive points to the actual folder where your website files are stored. For example:

    root /var/www/your-site-folder;
    

    Double-check the path is correct—typos here are super common.

  • Fix file/directory permissions
    Nginx runs as the www-data user by default, so your site files need to be accessible to this user. Run these commands in your server terminal to set the right ownership and permissions:

    sudo chown -R www-data:www-data /var/www/your-site-folder
    sudo chmod -R 755 /var/www/your-site-folder
    

    This ensures Nginx can read and serve your files.

  • Check the index directive
    Make sure your Nginx config includes the index file your site uses (like index.php, index.html). Add it to the server block if missing:

    index index.php index.html index.htm;
    

    If your site uses a custom index filename (like home.html), add that too.

  • Confirm an index file exists
    Even if the config is right, if there's no index file in your root directory, Nginx will throw a 403. Double-check that your main entry file (e.g., index.html or index.php) is present in the root folder you specified.

  • Restart Nginx to apply changes
    After adjusting the config or permissions, restart Nginx to make sure the changes take effect:

    sudo systemctl restart nginx
    

Fixing the "Character encoding not declared" Warning

This warning can cause garbled text if your site uses non-ASCII characters. Here's how to fix it:

  • Add a charset meta tag to your HTML
    In the <head> section of every HTML file, add this line to explicitly declare UTF-8 encoding:

    <meta charset="UTF-8">
    

    This tells browsers exactly which encoding to use for the page.

  • Set charset headers in PHP (if using PHP)
    If your site uses PHP, add this line at the very top of your PHP files (before any HTML output) to send a charset header:

    <?php header('Content-Type: text/html; charset=utf-8'); ?>
    

    This ensures the server sends the correct encoding info with the response.

  • Configure Nginx to set default charset
    You can also set a global charset in your Nginx config to apply it to all responses. Add this line inside your http block (in /etc/nginx/nginx.conf) or your site's specific server block:

    charset utf-8;
    

    Restart Nginx after making this change.

内容的提问来源于stack exchange,提问作者olivierhsta

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 04:31:09