Installation and Configuration of Nginx under CentOS

Step 1: Make sure that gcc-c++, pcre, zlib, openssl are installed on your system

If not, you can use the following commands to reload all of them

yum install gcc-c++  
yum install pcre pcre-devel  
yum install zlib zlib-devel  
yum install openssl openssl--devel  

Step 2: Check if nginx is installed

find -name nginx

If the system already has nginx installed, uninstall it first

yum remove nginx

Then download the latest version of nginx from the website and unzip it to find a place to store it, usually under / usr/local

Step 3: Install nginx

./configure  Installed by default/usr/local/nginx   
make  
make install

Take a look at the version of nginx and see if there are any problems

/usr/local/nginx/sbin/nginx -V

Start and restart Nginx

Start:

/usr/local/nginx/sbin/nginx 

restart

/usr/local/nginx/sbin/nginx -s reload

Step 4: Configure nginx

(1) Load Balancing

...
http{
...
    #Cluster of servers  
    upstream  tomcats.com {  #Server Cluster Name   
        server   127.0.0.1:8080  weight=5;#Server configuration weight means weight, the larger the weight, the greater the probability of allocation.  
        server   127.0.0.1:28080  weight=5;  
    }   


     server {
       ...
        location / {
            root   index.html;
            proxy_pass http://tomcats.com;  #Name of the corresponding server cluster
            index index.html;          
        }
...
}
...

Write the prepared cluster address into upstream to assign user access probability according to the specific gravity nginx. I have now set up two tomcat servers with 5 and 5 settings, equivalent to 10 users visiting my website. Five of them will go to the 8080 port tomcat server, and five of them will go to the 28080 port tomcat server, reducing the pressure on each server.

(2) Static Resource Management

...
http{
...
    server{
       ...
        location /assets/ {               #Request Address
             root /usr/local/resources;   #Root directory where resources exist
             expires    30d;       #Lifetime 30 days
             #autoindex on;        #Allow the entire directory to be displayed
        }

        location /scripts/ {
             root /usr/local/resources;
             expires   30d;
             #autoindex on;
        }

        location /styles/ {
             root /usr/local/resources;
             expires    30d;
             #autoindex on;
        }

        location /fonts/ {
             root /usr/local/resources;
             expires    30d;
             #autoindex on;
        }

       ...
    }
...
}
...

Web site developers can manage frequently updated files in static resources, such as js files in static resources, so that js users who request a site visit will not go to service requests, but will go directly to static resources to get them, which can reduce the pressure on services.

Operation:
Create a resource/scripts file directory under / usr/local/ and place the js file in it. You also need to create a scripts folder under / usr/local/nginx, which can be empty, but with the same name as the scripts under resources.Other static resources, such as pictures and fonts, can be put together or sorted out separately, which is a custom of your own.

This is my own nginx configuration file and now uses only load balancing and static resources

This is my own nginx configuration file and now uses only load balancing and static resources

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    #Cluster of servers  
    upstream  tomcats.com {  #Server Cluster Name   
        server    127.0.0.1:8080  weight=5;#Server configuration weight means weight, the larger the weight, the greater the probability of allocation.  
        server    127.0.0.1:28080  weight=5;  
    }   


    server {
        listen       80;           #Listening Port
        server_name  www.xxx.cn;   #Site Entry Address

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   index.html;
            proxy_pass http://tomcats.com;
            index index.html;
          # try_files $uri $uri/ /index.html =404;
        }

        location /assets/ {
             root /usr/local/resources;
             expires    30d;
             #autoindex on;
        }

        location /scripts/ {
             root /usr/local/resources;
             expires   30d;
             #autoindex on;
        }

        location /styles/ {
             root /usr/local/resources;
             expires    30d;
             #autoindex on;
        }

        location /fonts/ {
             root /usr/local/resources;
             expires    30d;
             #autoindex on;
        }

        #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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

Keywords: Nginx yum PHP zlib

Added by djfox on Mon, 20 May 2019 03:44:10 +0300