Author: Unclear careless, source: blog.csdn.net/qq_37142346…
Official account: Java Xiaokaxiu, website: Javaxks.com
When we set up a website, sometimes tend to load more pictures, if all from tomcat server to get the static resources, this will increase our server load, the server is running very slow, then we can use nginx server to load the static resources, so that you can realize the load balance, Decompression for our Tomcat server. This is what most large sites do, they have a separate image server, and here we are using Nignx to build a simple image server locally.
Install NignX
Nignx is a green version, as long as the official website to download decompression can be started, decompression directory as shown below:
Here we can use the command line start nignx.exe to start the server, also can use the bat batch file to start, open the batch file simple processing can start:
Both of the above methods can start the server. When we start the server, let’s test it briefly. Enter localhost in the browser and you will see a simple Nignx server welcome page.
Conf file. Nginx. conf consists of several blocks. The outer block is main. Main contains events and HTTP, HTTP contains multiple upstreams and servers, and server contains multiple locations:
Main (global Settings), server (virtual host Settings), upstream (load balancing server Settings), and location (URL matches specific location Settings). 1. Instructions set in the main block will affect all other Settings; 2. The instructions of the server block are mainly used to specify hosts and ports. (3) Upstream directive is used for load balancing, setting up a series of back-end servers; 4. The Location block is used to match the location of the page. The relationship between the four is that server inherits from Main, location inherits from server, and upstream neither inherits nor is inherited from other Settings. In these four parts, each part contains several instructions, which mainly include Nginx main module instruction, event module instruction, HTTP core module instruction, and each part can also use other HTTP module instruction. For example, Http SSL module, HttpGzip Static module, and Http Addition module.
After a brief introduction to the nignx configuration file, let’s take a look at its contents and start the configuration:
server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php${# proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php${# root HTML; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #}}Copy the code
This is part of the configuration file. Let’s look at the location node. Root below it represents the root directory of the site. Root HTML means there is an HTML folder in the root directory. By default, it accesses the index.html page of the HTML file in the directory, so you can see how to configure a custom image server. We can specify a server node, and its localhost node is designated as our image address domain. This gives us easy access to our static resources.
location / {
root html;
index index.html index.htm;
}
Copy the code
Second, set up the picture server
After the introduction of the above we should understand its general principle, let’s combat configuration. First of all, we have a local setup, so we don’t have a domain name. Here we can use any second level domain name as an exercise (for example, images.shinelon.com). We found a hosts file in C:\Windows\System32\drivers\etc to add the mapping to our domain name. Add the following configuration:
127.0.0.1 images.shinelon.com
Copy the code
Then we copy the server node of the nignx.conf file and change it to the following configuration:
server { listen 80; server_name images.shinelon.com; #access_log logs/host.access.log main; location / { root D:\images; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }}Copy the code
Above, we changed the domain name to our custom image domain, and changed the root directory to our image directory (D:\images), so that we can start the Nignx server to access the image resources. Here we add an image 1.jpg in this directory. When you enter in your browser images.shinelon.com/1.jpg can access our picture resources.
At this point, we set up a simple picture server.