Linux uses nginx mapping to specify directory files for external access

Modify the configuration file

#user www-data;
#将user www-data;注掉改为root
user root;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    
    
	worker_connections 768;
	# multi_accept on;
}

http {
    
    

	##
	# Basic Settings
	##

	sendfile on;
	tcp_nopush on;
	types_hash_max_size 2048;
	# server_tokens off;

	# server_names_hash_bucket_size 64;
	# server_name_in_redirect off;

	include /etc/nginx/mime.types;
	#default_type application/octet-stream;
	#修改默认类型为json
	default_type  application/json;


	##
	# SSL Settings
	##

	ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
	ssl_prefer_server_ciphers on;

	##
	# Logging Settings
	##

	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;

	##
	# Gzip Settings
	##

	gzip on;


	server {
    
    
		#配置跨域问题
  		add_header Access-Control-Allow-Origin *;
  		add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
  		add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
 
  		if ($request_method = 'OPTIONS') {
    
    
   			return 204;
  		}
        listen       80;
        server_name  localhost;
 		#该配置表示,自动拦截以/json/开头的请求,如果没有其他location的拦截配置能够符合该url,则命中该location。autoindex on表示开启文件索引,这样我们在浏览器输入文件目录url时,可以显示文件索引目录。最终访问的资源映射到了/root/目录。
        location /json/ {
    
    
   			default_type application/json;
   			autoindex on;
   			alias /root/json/;
			#root /root/json/;
        }
    }

	include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;
}

After modifying the configuration file, restart nginx;
the access format is as follows:
http://ip or domain name:port/json/xxxx.xxx

Report 403 error

access denied. Then it is a permission issue;
according to the official Nginx documentation, the Nginx program has two processes:

One is the master process, that is, the main process, and there is only one in the system. The main purpose of the master process is to read and evaluate configuration files, and to maintain worker processes.

One is the worker process, that is, the working process, and there can be more than one in the system. The worker process performs the actual processing of the request. NGINX relies on operating system-dependent mechanisms to efficiently distribute requests among worker processes. The number of worker processes is defined by the worker_processes directive in the nginx.conf configuration file and can be set as a fixed number or configured to automatically adjust to the number of available CPU cores.
Use the following command to directly view the Nginx process in the system:

ps aux|grep nginx|grep -v grep

insert image description here
See a master and a worker. Then the authority of the master is root, and the authority of the worker is www-data.
The solution is to change the configuration file of nginx to root, as follows:
1. In the head of /etc/nginx/nginx.conf, there is a line user www-data, and change www-data to root. Files stored in /root/json can be uploaded via HTTP access.
insert image description here
2. Put the file you want to access into the folder that www-data can access, and change the owner of the file below to www-data.

Static resource reports 404 error

All files are stored under /root/json/;

 location /json/ {
    
    
   			default_type application/json;
   			autoindex on;
			root /root/json/;
        }

The root configuration will follow the configured directory with the URL to form the corresponding file path, that is, the address you want to visit is:

https://xxxxx/json/a.json

The file path that nginx takes according to the configuration is

/root/json/json/a.json

The correct path is

/root/json/a.json

Nginx provides another static path configuration: alias configuration

Official root configuration

Sets the root directory for requests. For example, with the following configuration
location /i/ {
    
    root /data/w3;
}
The /data/w3/i/top.gif file will be sent in response to the “/i/top.gif” request

Official alias configuration

Defines a replacement for the specified location. For example, with the following configuration
location /i/ {
    
    alias /data/w3/images/;
}
on request of “/i/top.gif”, the file /data/w3/images/top.gif will be sent.

When accessing /i/top.gif, root requests the file to /data/w3/i/top.gif, and alias requests to /data/w3/images/top.gif, that is to say

Root response path: configured path + full access path (full location configuration path + static files)

The path of alias response: configuration path + static file (remove the path configured in location)

Solution:

 location /json/ {
    
    
   			default_type application/json;
   			autoindex on;
			alias /root/json/;
        }

Note: When using alias, you must add "/" after the directory name; in general, configure root in location/, and configure alias in location /*.

Guess you like

Origin blog.csdn.net/god_sword_/article/details/129341469