Record the first installation and configuration of Nginx

foreword

Although it is not the first time to contact Nginx, but it is rarely used. Today it is a complete installation and configuration. I wanted to write "Nginx Installation and Common Configuration" some time ago . Due to unexpected situations, I did not finish writing it. I will add it today. .

install nginx

The installation of nginx is relatively convenient. The environment I installed is Ubuntu 20.04 with root privileges. The installation only requires the following two commands:

apt update
apt install nginx

After the installation is started by default, the command to query the running statussystemctl status nginx

# systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2023-03-14 21:09:35 CST; 2h 55min ago
       Docs: man:nginx(8)
    Process: 943307 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 943314 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 943315 (nginx)
      Tasks: 5 (limit: 9132)
     Memory: 10.6M
     CGroup: /system.slice/nginx.service
             ├─943315 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ├─943316 nginx: worker process
             ├─943317 nginx: worker process
             ├─943318 nginx: worker process
             └─943319 nginx: worker process

Mar 14 21:09:35 w-mi-dev systemd[1]: Starting A high performance web server and a reverse proxy server...
Mar 14 21:09:35 w-mi-dev snoopy[943307]: [hostname:w-mi-dev ssh:((undefined)) uid:0 username:root pid:943307 ppid:1 rpname:(nginx) sid:943307 tty:(none) cwd:/ filename:/usr/sbin/nginx]: /usr/sbin/ng>
Mar 14 21:09:35 w-mi-dev snoopy[943314]: [hostname:w-mi-dev ssh:((undefined)) uid:0 username:root pid:943314 ppid:1 rpname:(nginx) sid:943314 tty:(none) cwd:/ filename:/usr/sbin/nginx]: /usr/sbin/ng>
Mar 14 21:09:35 w-mi-dev systemd[1]: Started A high performance web server and a reverse proxy server.

If you want to test it, you can access it through a browser http://YOUR_IP, and you will usually see an interface with the words "Welcome to nginx", but this is useless to me, and my default port cannot be accessed.

Find the location of nginx installation and configuration files

My nginx is installed in /usr/sbin/nginxthe directory , and the main directory of the configuration file is there /etc/nginx/nginx.conf. The way to find it is relatively simple. Use whichthe command

# which nginx
/usr/sbin/nginx

When looking for a configuration file, use nginx to check -tthe parameters

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If you can't find it with whichthe command , you can use to ps -ef | grep nginxfind the process id, and use ll /proc/nginx_pidto find the installation location.

Modify the configuration file

Enter /etc/nginxthe directory , you can see multiple configuration files and directories

# cd /etc/nginx/
# ll
total 64
drwxr-xr-x 2 root root 4096 Mar 14 20:56 conf.d/
-rw-r--r-- 1 root root 1077 Feb  4  2019 fastcgi.conf
-rw-r--r-- 1 root root 1007 Feb  4  2019 fastcgi_params
-rw-r--r-- 1 root root 2837 Feb  4  2019 koi-utf
-rw-r--r-- 1 root root 2223 Feb  4  2019 koi-win
-rw-r--r-- 1 root root 3957 Feb  4  2019 mime.types
drwxr-xr-x 2 root root 4096 Nov 10 14:38 modules-available/
drwxr-xr-x 2 root root 4096 Mar  9 21:30 modules-enabled/
-rw-r--r-- 1 root root 1490 Feb  4  2019 nginx.conf
-rw-r--r-- 1 root root  180 Feb  4  2019 proxy_params
-rw-r--r-- 1 root root  636 Feb  4  2019 scgi_params
drwxr-xr-x 2 root root 4096 Mar  9 21:30 sites-available/
drwxr-xr-x 2 root root 4096 Mar  9 21:30 sites-enabled/
drwxr-xr-x 2 root root 4096 Mar  9 21:30 snippets/
-rw-r--r-- 1 root root  664 Feb  4  2019 uwsgi_params
-rw-r--r-- 1 root root 3071 Feb  4  2019 win-utf

Where nginx.confis the main configuration file with the following content:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    
    
        worker_connections 768;
        # multi_accept on;
}

http {
    
    

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        # Gzip Settings
        ##

        gzip on;

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}


#mail {
    
    
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
    
    
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
#
#       server {
    
    
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

This file is the default configuration file, we can ignore it, just pay attention to a few points:

  • access log at/var/log/nginx/access.log
  • The error log is in/var/log/nginx/error.log
  • The main configuration file contains all configurations in /etc/nginx/conf.d/*.confthis directory

So /etc/nginx/conf.d/we create a new project.conffile in the directory and write the following content:

upstream login_entrance {
    
    
    server localhost:4101;
    server localhost:4102;
}

upstream exit_entrance {
    
    
    server localhost:6101;
    server localhost:6102;
}

server {
    
    
    listen       4100;
    server_name  localhost;

    location / {
    
    

        proxy_pass http://login_entrance;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

server {
    
    
    listen       6100;
    server_name  localhost;

    location / {
    
    

        proxy_pass http://exit_entrance;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

server {
    
    
    listen      8100;
    server_name localhost;

    location / {
    
    
        root    /usr/share/nginx/html;
    }
}

The meaning of the configuration file:

  • Visit port 8100 to see the nginx welcome page
  • Forward data sent to port 4100 to ports 4101 and 4102
  • Forward data sent to port 6100 to ports 6101 and 6102

Restart the nginx service

To restart, you can turn off the server and turn it on again, or you can directly use the following command:

  • systemctl restart nginx
  • nginx -s reload

View error

When nginx fails to start, you can use systemctl status nginxthe command to view it, or you can directly view the errorlog file. For example, you may see the following content:

2023/03/14 20:51:06 [emerg] 937503#937503: bind() to 0.0.0.0:4100 failed (98: Address already in use)
2023/03/14 20:51:06 [emerg] 937503#937503: bind() to 0.0.0.0:4100 failed (98: Address already in use)
2023/03/14 20:51:06 [emerg] 937503#937503: bind() to 0.0.0.0:4100 failed (98: Address already in use)
2023/03/14 20:51:06 [emerg] 937503#937503: bind() to 0.0.0.0:4100 failed (98: Address already in use)

You should know at a glance that port 4100 is occupied.

load balancing configuration

Load balancing is a common application of nginx, you only need to add some parameters to the configuration file:

polling

This is the default strategy, assigning each request to different available servers one by one in order, if the assigned server is unavailable, it will be automatically assigned to the next one

upstream good.cc {
    
    
    server localhost:4101;
    server localhost:4102;
}

Weights

The default value of weight is 1. The larger the value, the greater the probability of being accessed. In the following configuration, the access probability of port 4102 is twice that of 4101.

upstream good.cc {
    
    
    server localhost:4101 weight=1;
    server localhost:4102 weight=2;
}

least connection

As the name implies, the request is assigned to the server with the least number of connections.

upstream good.cc {
    
    
    least_conn;
    server localhost:4101;
    server localhost:4102;
}

IP hash

Assign according to the hash value of the access client ip, so that the requests of the same client will be assigned to the same server, which is suitable for stateful services

upstream good.cc {
    
    
    ip_hash;
    server localhost:4101;
    server localhost:4102;
}

Summarize

  • nginx is very easy to install on Ununtu 20.04, apt install nginxjust
  • The default main configuration file of nginx /etc/nginx/nginx.confwill contain conf.d/*.confall configurations in the same directory
  • nginx service can be viewed systemctl status nginxwith
  • How to restart nginx systemctl restart nginxornginx -s reload
  • As a load balancing server, nginx has multiple configuration methods such as round robin, weight, least connection, ipHash, etc.
  • I am also just getting started, and there are still many things to learn about usage methods and configuration files. Let’s summarize slowly later

==>> Anti-climbing link, please do not click, it will explode on the spot, and we will not be responsible for it! <<==

There is no good and no disgusting body, there are good and malicious actions, knowing good and knowing evil is conscience, doing good and eliminating evil is investigating things, just make up the number~

Guess you like

Origin blog.csdn.net/shihengzhen101/article/details/129543700