Nginx configuration image server
background
When we do projects in our daily life, we sometimes often encounter where do we need to put pictures? Sometimes third-party image storage is used, such as Tencent Cloud's COS and Alibaba Cloud's OOS. Of course, sometimes we also need to put it on a Linux server, so if we put it on the server, how should we directly access the image resources?
There are two ways to implement it. The first one is to use Tomcat to deploy image resource servers. We have already implemented this method. If you are interested, you can read my Tomcat image resource server article .
The second is that we can configure the image server through Nginx. (The premise is that Nginx has been configured on the server. If not, you can read my article, How to configure Nginx server on Linux )
request example
For example, we access pictures through IP http://你的IP:8089/1.gif
; we can access them directly in the browser, such as:
Nginx configuration image server
We need to go to the Nginx directory, find the nginx.conf file, and create a new server for modification.
server {
listen 8089;
server_name 你的IP;
location ~ .*\.(gif|jpg|jpeg|png)$ {
root /root/tomcat/img/; //你的图片资源路径
autoindex on; //自动开启目录访问权限
index index.html;
}
}
Then we enter nginx -t
the command to check nginx.conf
whether there is a problem with the file;
if it shows successful, there is no problem, as shown below:
Finally, we need to restart Nginx, enter the following command:
systemctl restart nginx.service
problem exists
After we deploy directly according to the above operations, if we visit, there may be 403 errors. Of course, there are no errors. That is the best. If 403 errors occur, we need to check which nginx startup user is. Enter the following command to view the following results:
ps aux|grep nginx
This is normal on my side. If your startup user is the same as mine, then this is not the problem, you can skip it. If yours is different from mine, there is a problem with the startup user. We can use nginx. Add such a sentence to the conf file. can be solved.
user root;
Then, we restart nginx and visit again, and it will be successful.