Reprint https://blog.csdn.net/ouyang111222/article/details/53266107
Foreword:
Traditional web projects generally deploy static resources together with projects in containers (such as tomcat, jetty), but sometimes these static resource files need to be taken out separately, and ngnix can act as a static resource server at this time.
Configure Nginx/Tengine
Please make sure that Nginx or Tengine is installed on your server (this article uses Tengine as an example)
Copy static resource files to a specified directory, such as /home/admin
Configure the nginx-proxy.conf file
server {
listen 8089;
server_name localhost;
location /resource_static/ {
root /home/admin/;
}
}
The listening port configured in this article is 8089, depending on the situation
- Test verification
The above configuration means that when you enter localhost:8089/resource_static/, you will access the /home/admin/resource_static/ directory of the local machine, and create a new file test.json under /home/admin/resource_static/, as shown below:
Enter in your browser:
localhost:8089/resource_static/test.json
cross domain problem
Cross-domain problems are often encountered, such as the following errors:
Access to Font at 'http://xxx:8089/resource_static/console/hello.ttf' from origin 'http://xxx:8089' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://xxx:8080' is therefore not allowed access.
Solution:
location /resource_static/ {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
root /home/admin/;
}
Reference https://michielkalkman.com/snippets/nginx-cors-open-configuration.html
If configured like this, there will still be problems
location /resource_static/ {
root /home/admin/;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
- ok