1024programmer Nginx Nginx configuration file content explanation

Nginx configuration file content explanation

----------------nginx Configure gzip compression

Under normal circumstances, the size of compressed html, css, js, php, jhtml and other files can be reduced to 25% of the original size, that is to say, the original 100k html is only 25k after compression. This will undoubtedly save a lot of bandwidth and reduce the load on the server.
It is relatively simple to configure gzip in nginx

In general, just add the following lines of configuration to the http section of nginx.conf

gzip on;
gzip_min_length 1000;
gzip_buffers 4 8k;
gzip_types text/plain application/x-Javascript text/css text/html
application/xml;

restart nginx
You can use the webpage gzip detection tool to detect whether gzip is enabled on the webpage
http://gzip.zzbaike.com/

—————How to redirect nginx error page
error_page 404 /404.html;
This 404.html is guaranteed to be in the html directory under the nginx main directory. If you need to jump directly to another address after a 404 error occurs, you can directly set it as follows:

error_page 404 http://www.***.net;

In the same way, common errors such as 403 and 500 can be defined.

Please note that the page size of the 404.html file must exceed 512k, otherwise it will be replaced by the default error page of ie by the ie browser.

——————————Virtual host configuration

server {
listen 80;
server_name localhost;
access_log /var/log/nginx/localhost.access.log;

location / {
root /var/www/nginx-default;
index index.php index.html index.htm;
}

location /doc {
root /usr/share;
autoindex on;
allow 127.0.0.1;
deny all;
}

location /images {
root /usr/share;
autoindex on;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
/var/www/nginx-default$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}

server {
listen 80;
server_name sdsssdf.localhost.com;
access_log /var/log/nginx/localhost.access.log;

location / {
root /var/www/nginx-default/console;
index index.php index.html index.htm;
}

location /doc {
root /usr/share;
autoindex on;
allow 127.0.0.1;
deny all;
}

location /images {
root /usr/share;
autoindex on;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
/var/www/nginx-default$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}

———————- Monitoring

location ~ ^/NginxStatus/ {

stub_status on; #Nginx status monitoring configuration
}

In this way, the running information of Nginx can be monitored through http://localhost/NginxStatus/ (the last / cannot be dropped):

Active connections: 1
server accepts handled requests
1 1 5
Reading: 0 Writing: 1 Waiting: 0

The content displayed by NginxStatus means as follows:
active connections ? The current number of active connections being processed by Nginx.
server accepts handled requests — a total of 14553819 connections were processed and successfully created
14553819 handshakes (to prove that there is no failure in the middle), a total of 19239266 requests were processed (an average of 1.3
data requests).
reading — nginx reads the number of header information from the client.
writing — the number of Header information returned by nginx to the client.
waiting — When keep-alive is turned on, this value is equal to active – (reading +
writing), which means that Nginx has finished processing the resident connection waiting for the next request instruction.

——————————- Static file processing
Through regular expressions, we can make Nginx recognize various static files

location ~ \.(htm|html|gif|jpg|jpeg|png|bmp|ico|css|js|txt)$
{
root /var/www/nginx-default/html;
expires 24h;
}
For images, static HTML files, js script files and css style files, etc., we hope that Nginx
Directly process and return to the browser, which can greatly speed up the speed of web browsing. So for this kind of files we need to pass root
command to specify the storage path of the file, and because such files are not often modified, the expires command is used to control its cache in the browser to reduce unnecessary requests.
The expires directive can control the ” Expires ” and ” Cache-Control
” (to control page caching). You can write Expires in the following format, for example:

expires 1 January, 1970, 00:00:01 GMT;
expires 60s;
expires 30m;
expires 24h;
expires 1d;
expires max;
expires off;

In this way, when you enter http://192.168.200.100/1.html, it will automatically jump to var/www/nginx-default/html/1.html

For example, all requests under the images path can be written as:
location ~ ^/images/ {
root /opt/webapp/images;
}

————————Dynamic page request processing [cluster]
Nginx itself does not support the popular JSP, ASP, PHP, PERL and other dynamic pages, but it can send requests to the back-end server through reverse proxy, for example
Tomcat, Apache, IIS, etc. to complete the request processing of dynamic pages. In the previous configuration example, we first defined the
After processing some static file requests directly, all other requests are sent to the backend server through the proxy_pass directive (in the above example it is
Tomcat). The simplest proxy_pass usage is as follows:

location / {
proxy_pass http://localhost:8080;
proxy_set_header X-Real-IP $remote_addr;
}

Here we do not use the cluster, but send the request directly to the Tomcat service running on port 8080 to complete similar JSP and Servlet
request processing.
When the number of page visits is very large, multiple application servers are often required to jointly undertake the execution of dynamic pages. At this time, we need to use a cluster architecture. Nginx passed
upstream command to define a cluster of servers. In the complete example above, we defined a server named tomcats
The cluster, which includes three servers and a total of six Tomcat services. And the proxy_pass command becomes:

# Configuration information of all background servers in the cluster
upstream tomcats {
server 192.168.0.11:8080 weight=10;
server 192.168.0.11:8081 weight=10;
server 192.168.0.12:8080 weight=10;
server 192.168.0.12:8081 weight=10;
server 192.168.0.13:8080 weight=10;
server 192.168.0.13:8081 weight=10;
}
location / {
proxy_pass http://tomcats;# reverse proxy
include proxy.conf;
}

———————-Stress Test
wget
http://blog.s135.com/soft/linux/webbench/webbench-1.5.tar.gz
tar zxvf webbench-1.5.tar.gz
cd webbench-1.5
make && make install
#webbench -c 100 -t 10 http://192.168.200.100/info.php
Parameter description: -c indicates the number of concurrency, -t indicates the duration (seconds)

root@ubuntu-desktop:/etc/nginx/sites-available# webbench -c 100
-t 10 http://192.168.200.100/info.php
Webbench – Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://192.168.200.100/info.php
100 clients, running 10 sec.

Speed=19032 pages/min, 18074373 bytes/sec.
Requests: 3172 susceed, 0 failed.

-------------------------------PPC provides nginx detailed configuration instructions

#running user
user nobody nobody;
#start process
worker_processes 2;
#Global error log and PID file
error_log logs/error.log notice;
pid logs/nginx.pid;
#Working mode and connection limit
events{use epoll;
worker_connections 1024;}#Set the http server, use its reverse proxy function to provide load balancing support

http{#Set mime type
include conf/mime.types;
default_type application/octet-stream;
#Set log format
log_format main ‘$remote_addr – $remote_user[$time_local]
””$request” $status $bytes_sent ””$http_referer”
“$http_user_agent” ””$gzip_ratio”‘;
log_format download ‘$remote_addr – $remote_user [$time_local]
””$request” $status $bytes_sent ””$http_referer”
“$http_user_agent” ””$http_range” “$sent_http_content_range”‘;
#Set request buffer
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;

#Open gzip module
gzip on;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain;
output_buffers 1 32k;
postpone_output 1460;

#Set access log
access_log logs/access.log main;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;

#Set load balancing server list
upstream mysvr{#weigth parameter indicates the weight value, the higher the weight value, the greater the probability of being assigned
#Squid on this machine opens port 3128
server 192.168.8.1:3128 weight=5;
server 192.168.8.2:80 weight=1;
server 192.168.8.3:80 weight=6;
}

#Set virtual host
server{listen 80;
server_name 192.168.8.1 www.okpython.com;
charset gb2312;
#Set the access log of this virtual host
access_log logs/www.yejr.com.access.log main;
#If accessing /img/*, /js/*, /css/* resources, take the local file directly without squid
#If there are many files, this method is not recommended, because the caching effect through squid is better
location ~ ^/(img|js|css)/ {
root /data3/Html;
expires 24h;
}
#Enable load balancing for “/”

location / {
proxy_pass http://mysvr;
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;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
#Set the address to view the status of Nginx
location /NginxStatus {
stub_status on;
access_log on;
auth_basic “NginxStatus”;
auth_basic_user_file conf/htpasswd; #The content of the conf/htpasswd file is used by apache
The provided htpasswd tool can be generated

}
}

xy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
#Set the address to view the status of Nginx
location /NginxStatus {
stub_status on;
access_log on;
auth_basic “NginxStatus”;
auth_basic_user_file conf/htpasswd; #The content of the conf/htpasswd file is used by apache
The provided htpasswd tool can be generated

}
}

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/nginx-configuration-file-content-explanation/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索