Nginx "403 Forbidden" error solution

Source: Internet
Author: User
Tags require


Nginx 403 Forbidden errors indicates that you are requesting a resource file, but nginx does not allow you to view it.
403 Forbidden is only an HTTP status code. Like 404,200, it is not a technical error.
Which scenarios require 403 status codes to be returned?

  1. The website prohibits a specific user from accessing all content. For example, the website shields access from an ip address.
    2. Access the directory where directory browsing is prohibited. For example, access the directory after autoindex is off.
    3. Users can only access files through the intranet.

The preceding common scenarios require 403 Forbidden.

Due to misconfiguration on the server, 403 Forbidden is returned if nginx does not want to return 403.

  1. Incorrect permission configuration

  2.  

This is the most common cause of nginx 403 forbidden.

To ensure correct file execution, nginx requires both the read permission of the file and the executable permission of all the parent directories of the file.

For example, when accessing/usr/local/nginx/html/image.jpg, nginxmust read/,/usr,/usr/local,/usr/local/nginx, the executable permissions of/usr/local/nginx/html.

Solution: set 755 for all parent directories and 644 for files to avoid incorrect permissions.

 
2. Incorrect directory index settings (index command configuration)

The root directory of the website does not contain the file set by the index command.

For example, for a website running PHP, index is usually configured like this

Index index.html index.htm index. php;

When you access this website, nginx searches for files in the root directory in the order of index.html, index.htm, and index. php. If none of the three files exist, nginx returns 403 Forbidden.

If index. php is not defined in index, nginx returns 403 Forbidden without checking whether index. php exists.

If you run jsp, you also need to add index. jsp and index. py to the index command.

Solution: add the homepage file to the index command. Common commands include index. php, index. jsp, index. jsp, and custom homepage files.

Nginx 403 forbidden is caused by two reasons: missing index files and permission issues. I met again today. By the way, let's make a summary.

1. Missing index.html or index. Php files

Server {listen 80; server_name localhost; index. php index.html; root/home/zhangy/www;


If no index.php or index.html is available under/home/zhang/www, the system will report 403 forbidden if the domain name cannot be found. For example, the index file specified by root does not exist when you access www.test.com.

2. Permission issues

403 of permissions are difficult to find, because I cannot afford it at the moment.

Server {listen 80; server_name localhost; index. php index.html; root/home/zhangy/www;


I put the web Directory under the user's directory, and the nginx startup user is nginx by default, so there is no read permission for the directory, so a 403 error will be reported. At this time, you can increase the permissions of the web directory, or change the nginx startup user to the user of the Directory.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.