This is the first day of my participation in the Gwen Challenge. For details, see: [Gwen Challenge](juejin.cn/post/696719…

Through the process of learning and sharing, I will summarize the problems and technologies in my work and output them. I hope that both the novice and the veteran can gain new knowledge through their own articles and put it into practice.

The introduction

Using Nginx to deploy the static file service, the Nginx service needs to be available before it can do anything else. If you don’t know how to deploy Nginx, you can refer to my previous article on the whole process guide for installing Nginx with yum. Those who have already been deployed can directly read the following content.

Nginx scenario introduction

Nginx configurations are numerous and complex, but there are only a few actual usage scenarios

  • Forward agent
  • The reverse proxy
  • Load balancing
  • Dynamic and static separation

There are different configuration files for different application scenarios. In the future, I will explain and present corresponding configuration files for different scenarios. Leave a hole here, haha.

Start the configuration

Returning to the current topic, we are going to configure a static file service for clients to access.

Linux defines an access directory

Create a files folder in the home directory to store files for clients to access

mkdir -p /home/files

Nginx adds a Location to expose the directory

If you install nginx using yum, there will be a default.conf file in /etc/nginx/nginx. conf

Default. conf is the default configuration file that exposes port 80. The default page is index.hmtl

Cat default.conf you can see the following figure. Add the configuration in the red box to the file

    location /download {
        alias   /home/files/;
        autoindex on;
    }
Copy the code

Add 127.0.0.1:80/download to the /home/files folder. autoindex on; Indicates to enable directory browsing

Verify the configuration and restart Nginx

1. Verify that the configuration file is correct

nginx -t
Copy the code

2. Restart nginx

nginx -s reload
Copy the code

Other Configuration Items

server { listen 80; Location /download {# go to Nginx alias /home/files; Autoindex on; # Display the approximate size of the file in kB or MB or GB autoindex_exact_size off; The default value is off, and the file time displayed is GMT. Autoindex_localtime on; Add_header cache-control no-store; Charset UTF-8, GBK; #add_header content-disposition attachment; }}Copy the code

Problems occurred during deployment

404

If a 404 error occurs, it is highly likely that the configuration file is faulty.

It could be location followed by a left slash /

There are also directory addresses that can be pointed to that do not exist

403

403 Forbidden stands for Forbidden, which is usually caused by three things

1. The directory permission is insufficient

Check directory permissions. Add permissions if you don’t have enough

chmod -R 755 /home/files
Copy the code

2. Nginx. conf user permissions are incorrect

vim /etc/nginx/nginx.conf
Copy the code

Change the user user name to user root or another high-privilege user name

3. The selinux configuration in Centos is not disabled

Check SELinux status:

If SELinux status is enabled, the SELinux function is enabled

/usr/sbin/sestatus -v 
Copy the code

How to close?

1. Temporary shutdown (no need to restart the machine) :

setenforce 0

2, permanent shutdown (to restart the machine)

vim /etc/selinux/config
Copy the code

Change SELINUX=enforcing to SELINUX=disabled

END

Welcome to pay attention to the public number programmer tool set 👍👍 is committed to sharing excellent open source projects, learning resources, common tools.

Reply keyword “attention package”, send you a complete map of programmer skills.