Nginx builds file download server

1. Set up a file server on this machine
1. Modify the configuration file:

server {
    
    
    listen       80;
    server_name  localhost;
    #防止乱码,需要加上编码
    #charset utf-8;
    #路由规则
    #如果想把nginx作为下载服务器,则改为系统目录地址
    #比如下面这样,(1)当访问主页时,打开的是本地的/data/upload/file目录
    location / {
    
    
        root   /data/upload/file;
        autoindex on;    #开启索引功能
        autoindex_exact_size off;  #关闭计算文件确切大小(单位bytes),
                                   #只显示大概大小(单位kb、mb、gb)
        charset 'utf-8'; #防止乱码,需要加上编码
        autoindex_localtime on;   #显示本机时间而非 GMT 时间
    }
 
    #location /file/ {     
		#root /data/icp/upload/;
		#charset 'utf-8';
		#autoindex on;
	#}
   
}
 

2. After modifying the configuration file, create the corresponding directory

3. Restart nginx and visit the page http://localhost:80/

Note: If a 403 error is reported when accessing the page, this is because of a permission issue. First, we have changed the user who starts nginx to be root, the highest authority account of root, so there is no user permission issue. Then the permission issue here is SELINUX Caused by it, just disable it. The method is to modify the configuration file "/etc/selinux/config"

4. When multiple access paths need to be configured, change root to alias for other paths:

	location /test {
    
    
        alias   /nginx/html/;#这里应该是alias,不再是root
        index   index.html;
    }

2. Nginx accesses files on another server
(1) Method 1
Server A accesses files in the directory of server B

1. Both servers need to install nginx, and the nginx configuration is as follows:

A server configuration:

#给出的file都没有匹配到,则重新请求最后一个参数给定的uri,就是新的location匹配
location ^~ /file/{
    
    
		try_files $uri @new_uploads;
}
location @new_uploads{
    
    
		proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://xxx.xx.xxx.xxx:9012;
}

B server configuration:

server {
    
    
        listen       9012;
        server_name  localhost;
		location ^~ /file/{
    
    
			alias  /home/file/;
			#autoindex on;(原配置)
            autoindex on;
			index  index.html index.htm;
		}
     }

2. Restart nginx on both servers

3. Access: A server IP: port/file/xxx to access the files in the B server/home/file/ directory.

(2) Method 2:
Server A accesses files in the directory of server B

1. Both servers need to install nginx, and the nginx configuration is as follows:

A server configuration:

location /file{
    
    
	proxy_pass http://172.16.42.100:8081/file;
	 client_max_body_size 5000m;
}

B server configuration:

server {
    
    
        listen       8081;
        server_name  localhost;
		 location  /file {
    
         
			root /data/icp/upload;
			charset 'utf-8';
			autoindex on;
			index  index.html index.htm;
		}
     }

2. Restart nginx on both servers

3. Access: A server IP: port/file/xxx to access the files in the B server/data/icp/upload/file/ directory.

Guess you like

Origin blog.csdn.net/jialiu111111/article/details/131726468