Author: JackTian wechat public ID: Jake_Internet

This is the first day of my participation in Gwen Challenge

What is Nginx?

Nginx is a high-performance HTTP and reverse proxy Web server that also provides IMAP/POP3 / SMTP services. It was developed by Igor Sassoyev for the second most visited rambler.ru site in Russia. The first public version 0.1.0 was released on October 4, 2004. It is characterized by less memory, strong concurrency, developed for performance optimization, stability and low system resource consumption, as well as high processing capacity for HTTP concurrent connections, which can support up to 50,000 concurrent connections per machine.

So, in fact, Nginx’s concurrency is better for the same type of web server, and in a real world, if we use Nginx, it might look like the following architecture diagram for this scenario. The Nginx server is a load balancer or reverse proxy server, so when a client sends a request to the Nginx server, The Nginx server needs to forward the requests from the client for its configured rules to LAMP, Tomcat, and LNMP on the backend.

Why Nginx?

As a Web server

Nginx uses fewer resources and supports more concurrent connections than Apache. In the case of high concurrency, Nginx is a substitute for Apache server. As a load balancing server, Nginx supports Rails and PHP programs as well as HTTP proxy servers. It is written in C language. It is much better than Perlbal in both system resource overhead and CPU efficiency.

Nginx configuration is simple, Apache complex

Nginx is easy to start, almost can do 7*24 hours uninterrupted operation, even if it has not been restarted for a long time, it can also upgrade the software version in the case of uninterrupted service, static processing performance is more than 3 times higher than Apache, Nginx needs to cooperate with other backend to use, Apache has simpler PHP support and more components than Nginx.

emphasis

Nginx is asynchronous, multiple connections can correspond to one process; Apache is a synchronous multi-process model, one connection corresponds to one process;

Good at field

Nginx is designed to handle static requests on the front end; Apache is suitable for handling dynamic requests at the back end;

Nginx installation

Installing Support Software

The configuration and operation of Nginx requires the support of PCRE and Zlib software packages. It is necessary to install these software development packages for the corresponding libraries and header files to ensure the smooth installation of Nginx.

# yum -y install pcre-devel zlib-devel
Copy the code

Create running users and groups

By default, the Nginx server runs as Noboby. It is recommended that you create a new user account to more accurately control access rights, increase flexibility, and reduce security risks.

# useradd -M -s /sbin/nologin nginx
Copy the code

Download compile install

When configuring Nginx compilation options, set the installation directory to /usr/local/nginx, and run user and group to Nginx. Enable the http_stub_status_module module to support status statistics for viewing server connection information.

# # wget http://nginx.org/download/nginx-1.17.0.tar.gz tar ZXF nginx - 1.17.0. Tar. Gz # CD nginx - 1.17.0 #. / configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module # make && make installCopy the code

Create link files for the main program Nginx

# ln -s /usr/local/sbin/nginx /usr/local/sbin/ls -l /usr/local/sbin/nginx LRWXRWXRWX. 1 root root 21 6月 4 07:31 /usr/local/sbin/nginx -> /usr/local/sbin/nginxCopy the code

After the installation is complete, go to the default installation path to the sbin directory and run nginx to start.

# cd /usr/local/nginx/sbin/
# pwd
/usr/local/nginx/sbin
# ./nginx 
# nginx
Copy the code

Listen for the state of Nginx programs

# netstat anpt | grep nginx TCP 0 0 0.0.0.0:0.0.0.0:80 * 53816 / LISTEN nginxCopy the code

After listening to the port, directly access the Nginx address in the browser. When the browser displays the following page, it indicates that Nginx has been installed successfully.

Use Nginx service scripts

Nginx service script can be written, using chkconfig and service tools for unified management;

#! /bin/bash # chkconfig: 2345 99 20 # description: Nginx Server Control Scripts shell PROG="/usr/local/nginx/sbin/nginx" PIDF="/usr/local/nginx/logs/nginx.pid" case "$1" in start) if [ -f $PIDF ]; then echo "Nginx is running.. Start it is error" else $PROG fi ;; stop) if [ -f $PIDF ]; then kill -s QUIT $(cat $PIDF) rm -rf $PIDF else echo "Nginx is stopping .. Stop it is error" fi ;; restart) $0 stop $0 start ;; reload) if [ -f $PIDF ]; then kill -s HUP $(cat $PIDF) else echo "Nginx is stopping . reload it is error" fi ;; status) if [ -f $PIDF ]; then echo "Nginx is running" else echo "Nginx is stopping" fi ;; *) echo "Usage: $0 (start|stop|restart|reload|status)" exit 1 esac exit 0 # chmod +x /etc/init.d/nginx # chkconfig --add nginxCopy the code

If you modify the Nginx configuration file, run the./ Nginx -s reload command to load the Nginx configuration file.

# ./nginx -s reload
Copy the code

conclusion

We have introduced the basic concepts of Nginx, why to use Nginx, and have a preliminary understanding of the installation of Nginx. The following articles will continue to introduce forward proxy, reverse proxy, load balancing and building LNMP architecture. Today we introduce here, have a question you feel free to leave a message to discuss oh.

Original is not easy, if you think this article is useful to you, please kindly like, comment or forward this article, because this will be my power to output more high-quality articles, thank you!