Nginx entry-level installation and basic use

Nginx is a high-performance http server/reverse proxy server and email (IMAP/POP3) proxy server. Developed by Russian programmer Igor Sysoev, the official test nginx can support 50,000 concurrent links, and the consumption of resources such as cpu and memory is very low, and the operation is very stable.

Application scenarios

  1. http server. Nginx is an http service that can independently provide http services. It can be a static web server .
  2. Virtual host . Multiple websites can be virtualized on one server. For example, virtual hosts used by personal websites.
  3. Reverse proxy, load balancing. When the number of website visits reaches a certain level, when a single server cannot meet the user's request, a cluster of multiple servers is required to use nginx as a reverse proxy. And multiple servers can share the load equally, and no one server will be idle due to a high load of one server.

 

nginx installation

Download nginx:

Official website:

http://nginx.org/

Required installation environment

  1. Need to install gcc environment. yum install gcc-c++
  2. Third-party development kits.   
  • PCRE

       PCRE (Perl Compatible Regular Expressions) is a Perl library, including Perl compatible regular expression libraries. The http module of nginx uses pcre to parse regular expressions, so the pcre library needs to be installed on linux.

        yum install -y pcre pcre-devel

Note: pcre-devel is a secondary development library developed by pcre. Nginx also needs this library.

  • zlib

       The zlib library provides a variety of compression and decompression methods. Nginx uses zlib to gzip the content of the http package, so you need to install the zlib library on Linux.

       yum install -y zlib zlib-devel

  • openssl

       OpenSSL is a powerful secure socket layer cryptographic library, including the main cryptographic algorithms, commonly used key and certificate packaging management functions and SSL protocol, and provides a wealth of applications for testing or other purposes.

       Nginx not only supports the http protocol, but also supports https (that is, HTTP is transmitted over the ssl protocol), so the openssl library needs to be installed in linux.

       yum install -y openssl openssl-devel

installation steps

Step 1: Upload the nginx source code package to the linux system

Step 2: Unzip

[root@localhost ~]# tar zxf nginx-1.8.0.tar.gz

Step 3: Use the configure command to create a makeFile file.

./configure && make && make install

 

Start nginx

Enter the sbin directory

[root@localhost sbin]# ./nginx

Close nginx:

[root@localhost sbin]# ./nginx -s stop

Recommended Use:

[root@localhost sbin]# ./nginx -s quit

Restart nginx:

  1. Turn off first and then start.
  2. Refresh the configuration file:

[root@localhost sbin]# ./nginx -s reload

Visit nginx

The default is port 80.

Note: Whether to turn off the firewall.

Configure virtual host

It is to start multiple websites on one server. How to distinguish different websites:

  1. Different domain names
  2. Different ports  
  1.  

Differentiate different virtual machines by port

Nginx configuration file:

/usr/local/nginx/conf/nginx.conf

Reload configuration file

[root@localhost nginx]# sbin/nginx -s reload

Nginx implements reverse proxy

Step 1: Install two tomcats, which run on ports 8080 and 8081 respectively.

Step 2: Start two tomcats.

Step 3: Reverse proxy server configuration

Load balancing

If a service is provided by multiple servers, the load needs to be distributed to different servers for processing, and load balancing is required.

upstream tomcat00 {
            server 192.168.25.128:8080;
            server 192.168.25.128:8081;
        }
    server {
        listen       80;
        server_name  www.test.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #root   html;
            proxy_pass http://tomcat00;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

The server weight can be adjusted according to the actual situation of the server. The higher the weight, the more requests are allocated, and the lower the weight, the fewer requests. The default is 1

upstream tomcat00 {
            server 192.168.25.128:8080;
            server 192.168.25.128:8081 weight=2;
        }

Guess you like

Origin blog.csdn.net/tonglei111/article/details/109051178