The previous article has introduced the role of Nginx and the installation of Nginx. In this article, we will look at a few practical examples to further understand Nginx.
(1) Introduction to some common configurations of Nginx
```` //The user running nginx should generally not be set to root user nginx // Worker process, usually equal to the number of CPUs worker_processes 1; //Global error log definition type, [ debug | info | notice | warn | error | crit ] error_log /var/log/nginx/error.log warn; //nginx process file pid /var/run/nginx.pid; events { //#The maximum number of connections for a single process (maximum number of connections = number of connections * number of processes) worker_connections 1024; } // set http general http { include /etc/nginx/mime.types; default_type application/octet-stream; //log log format log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; //path to access log access_log /var/log/nginx/access.log main; //The instruction specifies whether nginx calls the sendfile function (zero copy mode) to output the file sendfile on; #tcp_nopush on; //Long connection timeout, in seconds keepalive_timeout 65; //Whether to enable compression #gzip on; //Configuration file loaded at startup include /etc/nginx/conf.d/*.conf; //define a virtual host server{ // listen port number listen 8777; // Domain name configuration, there can be multiple, separated by spaces server_name 192.168.10.160 www.myhttp.com; //set encoding set charset utf-8; //resource request access configuration location / { //Define the directory for static resources root /usr/share/nginx/html; //Define the html home page to visit, you can configure multiple, if it does not take effect, visit the following one in turn index index.html index.htm; } //The status code of the error page and the error html page error_page 500 502 503 504 /50x.html; //500+ return definition of related errors location = /50x.html { root /usr/share/nginx/html; } } } ````
(2) Use Nginx to configure a static resource service
Using nginx to configure static resource file access is very simple, such as the home page access of html in the first step, the following is the static access configuration of a website, css, js, image and other related resources:
```` // location ~ ^/(images|javascript|js|css|flash|media|static)/ { root /root/ ; expires 3d;#cache validity period //autoindex on; #Open the directory browsing function //autoindex_exact_size on; #Display the file size in a humanized way, otherwise it will be displayed in bytes // autoindex_localtime on; #Display according to server time, otherwise display in gmt time allow 192.168.10.100; allow 172.29.73.0/24; deny all; } ````The above code needs to be configured in the server, and then you can access the static resources under /root/, and you can define the access of the relevant user ip.
In addition, we can also open the directory browsing function for the specified folder, so that we can easily access the required resources on the web page.
(3) Use Nginx+Html to configure a service that can play video by proxy
Using nginx+html, it is very convenient to build a video playback service. The default nginx here supports mp4 type video playback, but if there are other formats such as flv Then you need the relevant js support.
```` location ~ ^/(videos)/ { root /root/ ; autoindex on; #Open the directory browsing function mp4; mp4_buffer_size 1m; mp4_max_buffer_size 5m; limit_rate 260k; limit_rate_after 3m; autoindex_exact_size off; autoindex_localtime off; } ````
The html code is as follows:
```` <!DOCTYPE html> <html> <head> <title> play vedio</title> //js plugin vedio <link href="http://vjs.zencdn.net/5.0.2/video-js.css" rel="stylesheet"> <script src="http://vjs.zencdn.net/ie8/1.1.0/videojs-ie8.min.js"></script> <script src="http://vjs.zencdn.net/5.0.2/video.js"></script> </head> <body> //Play mp4 <video src="videos/a.mp4" controls="controls" width="500" height="400"></video> //flv playback <video id="example_video_1" class="video-js vjs-default-skin" controls preload="none" width="640" height="400" poster="http://vjs.zencdn.net/v/oceans.png" data-setup="{}"> <source src="videos/c.flv" type="video/flv"> </video> </body> </html> ````Video.js is used above to support the playback of flv video format.
(4) Use Nginx to configure reverse proxy + load balancing
The last function should be the most common scene in actual development. Let’s first look at a simple proxy forwarding ,
If we have a tomcat service now, the port number is 8888, the deployment ip is 192.168.10.161, and the
nginx ip is 192.168.10.160, then how to forward the request to the tomcat machine through nginx?
Simple proxy forwarding:
```` server{ //Listen on port 80 listen 80; //domain name server_name 192.168.10.160; location / { 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://192.168.10.161:8888/web/index; } //record application log access_log /var/log/nginx/my_proxy.log; ````
Through the above configuration, when users access http://192.168.10.160, they will automatically jump to http://192.168.10.161:8888/web/index, the tomcat backend service.
The following question is coming. One tomcat can no longer handle the current traffic request. We need to deploy another tomcat for load balancing. Suppose the ip is 192.168.10.162, then how to use Nginx for load balancing forwarding?
```` http{ upstream my_service { server 192.168.10.161:8888 server 192.168.10.162:8888 } server { listen 80; server_name www.backend.com; location / { proxy_pass http://my_service; // The back-end web server can obtain the user's real IP through X-Forwarded-For 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; } } } ````An example of load balancing is completed as above. Of course, there are many strategies for load balancing, including:
(1) polling
(2) weight
(3) minimum number of connections
(4) minimum processing time
(5) ip_hash
(6) fair
, etc. , there are a lot of extension plug-ins on github, students who are just interested can try it by themselves.
If you have any questions, you can scan the code and follow the WeChat public account: I am the siege division (woshigcs), leave a message in the background for consultation. Technical debts cannot be owed, and health debts cannot be owed. On the road of seeking the Tao, walk with you.