Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure and build Nginx website server in centos system

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

How to configure and build Nginx website server in centos system to solve this problem, today the editor summarizes this article about Nginx configuration, which can be used as a reference for interested friends. I hope it will be helpful to you.

I. Foundation of Nginx services

Nginx (engine x) is developed for performance optimization and is characterized by low memory footprint, stability and low system resource consumption, and high processing power for concurrent connections (5000 concurrent requests can be supported by a single physical server). In fact, the concurrency ability of nginx does perform well in the same type of web server. Chinese mainland uses nginx website users: Baidu, JD.com, Sina, NetEase, Tencent, Taobao and so on. IMAP/POP3/SMTP services are also provided.

Advantages of Nginx:

* * High concurrent connections * * officially tested that Nginx can support 50, 000 concurrent connections, while the actual production environment can support 20, 000 to 40, 000 concurrent connections. * * low memory consumption * * Nginx+PHP (FastCGI) server. Under 30,000 concurrent connections, starting 10 Nginx processes consumes 150MB memory and 15MB*10=150MB. 64 PHP-CGI processes that start consume 1280 memory and 20MB*64=1280MB, plus the memory consumed by the system itself, which consumes less than 2GB memory. * * low cost * * the purchase of hardware load balancer switches such as F5BIG-IP and NetScaler costs more than 100,000 RMB to hundreds of thousands of RMB. Nginx is open source software and uses 2-clause BSD-like protocol, which can be tried for free and can be used for commercial purposes. * * the configuration file is very simple * * the network is as easy to understand as the program, even if it is not dedicated to the system administrator. * * Rewrite rewriting is supported * * http requests can be divided into different backend server groups according to different domain names and URL. * * built-in health check feature * * if a Web server at the NginxProxy backend goes down, the access to the frontend will not be affected. * * Save bandwidth * * supports GZIP compression, and you can add Header headers cached locally by browsers. * * High stability * * is used for reverse proxy, and the probability of downtime is minimal. * * support hot deployment * * Nginx supports hot deployment, which is very easy to automate, and it can run almost 7 days * 24 hours a day without restarting, and can upgrade the software version with uninterrupted service.

The following figure shows the performance comparison of Nginx, Apache, and lighttpd:

Having said so much to highlight the powerful performance of Nginx, how to build a Nginx web server based on centos 7 (including the configuration of virtual web hosts), let's continue to explain the configuration of Nginx and its application on virtual machines:

2. Preparatory work:

One centos 7 server

One centos 7 system disk

3. Software packages that need to be used, link

Https://pan.baidu.com/s/1cfdQeNWAidd3XVtGisQU6g extraction code: usjt

4. It can also be downloaded from the official website http://www.nginx.org/

. Third, start to build the Nginx website (mount the system disk and install the required dependency packages. ):

1. The dependency packages required for installation are provided by the system disk:

2. Compile, install and configure optimized Nginx

[root@localhost media] # useradd-M-s / sbin/nologin nginx # create system user [root@localhost media] # tar zxf nginx-1.12.0.tar.gz-C / usr/src # unpack [root@localhost media] # cd / usr/src/nginx-1.12.0/ [root@localhost nginx-1.12.0] #. / configure-- prefix=/usr/local/nginx-- user=nginx-- group=nginx-- with-http_stub _ status_module & & make & & make install # compile and install Nginx [root @ localhost ~] # ln-s / usr/local/nginx/sbin/nginx / usr/local/sbin/# create the link file of the main program in order to enable the startup of the Nginx service Stop, reload and other operations are more convenient, you can edit the Nginx service script. The script is compiled as follows: [root@localhost ~] # vim / etc/init.d/nginx # Editing service script #! / bin/bash# chkconfig:-99 20PROG = "/ usr/local/nginx/sbin/nginx" PIDF= "/ usr/local/nginx/logs/nginx.pid" case "$1" in start) $PROG;; stop) kill-s QUIT $(cat $PIDF);; restart) $0 stop $0 start Reload) kill-s HUP $(cat $PIDF) *) echo "USAGE:$0 {start | stop | restart | reload}" exit 1esacexit 0 [root@localhost ~] # chmod + x / etc/init.d/nginx # add execution permission [root@localhost ~] # chkconfig-- add nginx # add as a system service [root@localhost ~] # systemctl start nginx # start Nginx service To confirm the normal operation of the script [root@localhost ~] # vim / usr/local/nginx/conf/nginx.conf # adjust the configuration file to optimize the web service .worker _ processes 2 # number of worker processes # error_log logs/error.log; # error log file location # error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid; # location of PID file events {use epoll; # add this line to even {} to improve performance worker_connections 4096 Each process handles 4096 connections}

The above optimizations are based on the global configuration, and the implications of each optimization are as follows:

Worker_processes: indicates the number of worker processes. If the server consists of multiple CPU blocks or uses multi-core processors, you can refer to the total number of CPU cores to specify the number of worker processes. The specific meaning is reflected in the worker_connections configuration item

Worker_connections: this configuration item specifies the connections handled by each process, which is generally less than 10000 (the default is 1024). It is associated with the configuration item of the number of worker processes above. For example, if the number of worker processes is 8 and each process handles 4096 connections, the number of connections that allow Nginx to provide services normally has exceeded 30,000 (409632768). Of course, it also depends on the performance of physical conditions such as server hardware and network bandwidth.

3. Build a virtual web host based on domain name:

1. HTTP configuration:

Nginx's configuration file uses the "http {}" delimiter to set the HTTP server, including access log, http port, web page directory, default character set, connection retention, and site global settings such as virtual web host, php parsing, and so on, most of which are contained in the subdelimiter "server {}". "server {}" represents a specific site setting.

[root@localhost ~] # vim / usr/local/nginx/conf/nginx.conf http {include mime.types; default_type application/octet-stream; log_format main'$remote_addr-$remote_user [$time_local] "$request"'$status $body_bytes_sent "$http_referer"'"$http_user_agent"$http_x_forwarded_for"' # remove the "#" sign access_log logs/access.log main; # access log location at the beginning of the above three lines sendfile on; turns on efficient file transfer mode # tcp_nopush on; # keepalive_timeout 0; keepalive_timeout 65; # connection persistence timeout # gzip on; server {listen 80 # web server listening port, you can use the form of "ip address: Port" server_name www.test1.com; # website domain name charset utf-8; # website default character set, you must remove the previous "#" number access_log logs/test1.access.log main # access log file name location / status {# add location / status to enable status statistics, access location is / status stub_status on; # turn on status statistics function access_log off; # turn off logging at this location} location / {root / var/www/test1 # website root directory index index.html index.php; # default home page, changed to index.php to support php pages};.. Error_page 500502 503 504 / 50x.hml; # feedback page for internal errors location = / 50x.html {# error page configuration root html;}

The above configuration only builds a website service. If you want to run more than one, copy the template provided at the end of the configuration file and paste it into the "server {}" configuration, because there are too many "{}" in the configuration file, so you need to copy it to the original "server {}" in order to avoid errors, as follows:

Server {listen 80; server_name www.test2.com; charset utf-8; access_log logs/test2.access.log main; location / status {stub_status on; access_log off;} location / {root / var/www/test2; index index.html index.php }} server {listen 80; server_name www.test1.com;.

At this point, the virtual host has been built, and you need to restart the service to take effect to verify the normal operation of the web server (DNS needs to be set up by yourself)

Fourth, access status statistics virtual host application

[root@localhost ~] # nginx-t # use this command to check the configuration file before restarting the service. # if there is an error in the configuration file, it will indicate which line the error is. # if it is correct, the OK will be displayed. If there is an error, the restart service will not report an error, but the configuration file will not take effect. Nginx: [emerg] unexpected ";" in / usr/local/nginx/conf/nginx.conf:44# indicates that there is an error on line 44: nginx: configuration file / usr/local/nginx/conf/nginx.conf test failed [root@localhost ~] # nginx-t # shows ok below, indicating no problem. Nginx: the configuration file / usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file / usr/local/nginx/conf/nginx.conf test is successful# prepare the website directory and test files, and establish the root directory for the two virtual web hosts respectively. And prepare to test the home page to make it easier to distinguish [root@localhost named] # mkdir-p / var/www/test1 [root@localhost named] # mkdir-p / var/www/test2 [root@localhost named] # echo "www.test1.com" > / var/www/test1/index.html [root@localhost named] # echo "www.test2.com" > / var/www/test2/index.html during the test

Client authentication:

① visits the home page of www.test1.com:

② accesses the status statistics page of www.test1.com:

The above meanings are as follows:

Active connections indicates that the current number of active connections is 2

Server accepts handled requests represents the connection information processed, with three digits indicating the number of processed connections, three successful handshakes, and six processed requests.

① visits the home page of www.test2.com:

② accesses the status statistics page of www.test2.com:

These are the steps to configure and build a Nginx website server in centos, and you need to use it yourself in order to understand the details. If you want to know more about it, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report