nginx website service

I. Overview Nginx is a high performance HTTP and reverse proxy server 1. Advantages and disadvantages: (1) The advantage...
Monitoring settings for web Services
Access State Statistics Configuration
Access Control Based on Authorized Password
Client-based access control
Domain name-based nginx virtual host
IP Address Based
Port-based

I. Overview

Nginx is a high performance HTTP and reverse proxy server
1. Advantages and disadvantages:

(1) The advantages of nginx over apache:

  • Lightweight, also web services, consumes less memory and resources than apache

  • Anti-concurrency, nginx processes requests asynchronously and non-blocking, while apache is blocked with high concurrency, nginx maintains low resource consumption and high performance

  • Highly modular design, relatively simple to write modules

(2) The advantages of Apache over nginx:

  • Rewrite is more powerful than nginx's rewrite###rewrite's main function is to achieve uniform resource locator (URL) jumps

  • There are so many modules, you can find everything you think of

  • Less bugs, relatively more bugs in nginx

  • Ultra stable

I/O for Linux 2

I/O means Input/Output in a computer
Disk I/O:buff Write/cache Read

Synchronization/Asynchronization: The focus is on the messaging mechanism, that is, whether the callee provides a notification of completion status while waiting for the result of one thing to be processed.

  • Synchronization: synchronous, the callee does not provide a notification message about the outcome of the event's processing, requiring the caller to actively ask if the process is complete
  • Asynchronous: asynchronous, where the callee actively notifies the callee of the state of operation through a status, notification, or callback mechanism

Blocking/non-blocking: Focus on the state of the caller before waiting for the result to return

  • blocking: blocking refers to IO operations that need to be completely completed before returning to user space. The caller is suspended for nothing else until the result of the call is returned.
  • Non-blocking: Non-blocking refers to returning an IO operation to the user immediately after it is invoked, without waiting for the IO operation to complete completely. The caller will not be suspended until the final result of the call is returned and can do something else.
3. Installation and Debugging

1. First, install the package and copy it to the system

Install Dependency Package

yum -y install pcre-devel zlib-devel gcc gcc-c++ make

New users and groups are easy to manage (the nginx service program runs as nobody by default and it is recommended to create a dedicated user account for it to control access more accurately)

[root@localhost nginx-1.12.0]# useradd -M -s /sbin/nologin nginx

Unzip the file under / opt

[root@localhost opt]# tar zxvf nginx-1.12.0.tar.gz -C /opt

Compile and install under the unzipped file

[root@localhost opt]# cd uginx-1.120 -bash: cd: uginx-1.120: No such file or directory [root@localhost opt]# cd nginx-1.12.0/ [root@localhost nginx-1.12.0]# ls auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src [root@localhost nginx-1.12.0]# ./configure \ > --prefix=/usr/local/nginx \ > --user=nginx \ > --group=nginx \ > --with-http_stub_status_module [root@localhost nginx-1.12.0]# make && make install

Establish soft links to services

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ Or join PATH [root@localhost nginx-1.12.0]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

Check grammar

[root@localhost nginx-1.12.0]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

start nginx

[root@localhost nginx-1.12.0]# nginx [root@localhost nginx-1.12.0]# ss -natp|grep 80 LISTEN 0 128 *:80 *:* users:(("nginx",pid=6319,fd=6),("nginx",pid=6318,fd=6))

Close nginx process

[root@localhost nginx-1.12.0]# Kill-3 6318 Must kill the parent process here [root@localhost nginx-1.12.0]# ss -natp|grep 80

Add nginx service
Method One

[root@localhost nginx-1.12.0]# cd /etc/init.d/ [root@localhost init.d]# vim nginx

[root@localhost init.d]# chmod +x nginx [root@localhost init.d]# chkconfig --add nginx to the startup item

Stop before and after starting the service

[root@localhost init.d]# ss -natp|grep 80 [root@localhost init.d]# service nginx start [root@localhost init.d]# ss -natp|grep 80 LISTEN 0 128 *:80 *:* users:(("nginx",pid=6609,fd=6),("nginx",pid=6608,fd=6)) [root@localhost init.d]# service nginx stop [root@localhost init.d]# ss -natp|grep 80

Method 2:

vim /lib/systemd/system/nginx.service [Unit] Description=nginx After=network.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/bin/kill -s HUP $MAINPID ExecStop=/usr/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target

The service will now be opened with the following system controls

systemctl start nginx

configuration file

[root@localhost init.d]# cd /usr/local/nginx/conf/ [root@localhost conf]# ls fastcgi.conf fastcgi_params koi-utf mime.types nginx.conf scgi_params uwsgi_params win-utf fastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default [root@localhost conf]# vim nginx.conf



You can change the maximum here to 65535 because of the port,
Here is the number of cores * Number of links per kernel = Number of connections serviced normally
Number of connections normally serviced query

Can be modified with ulimit-n number of connections

http { include mime.types; #Default file type 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"'; #Log formatting #access_log logs/access.log main; #Access log location sendfile on; #Supports file sending (downloading) tcp_nopush on; #This option allows or prohibits the use of socketde TCP_ Option for CORK (caching data before sending packets), which is used only when sendfile is used keepalive_timeout 0; keepalive_timeout 65; #Connection retention timeout in seconds gzip on; #Gzip module settings to set whether gzip compressed output is turned on

Monitoring settings for web Services

server { listen 80; #Listening Address and Port server_name localhost; #Site domain name, which can be multiple, separated by spaces #charset koi8-r; #Default Character Set for Web Pages #access_log logs/host.access.log main; location / { root html; #Site root location/usr/local/nginx/html index index.html index.htm; #Default Home Page File Name } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; #Internal error feedback page location = /50x.html { #Error Page Settings root html; }

New Page

[root@localhost ~]# cd /usr/local/nginx/ [root@localhost nginx]# ls client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp [root@localhost nginx]# cd html/ [root@localhost html]# ls 50x.html index.html [root@localhost html]# vim test.html

[root@localhost conf]# cp nginx.conf nginx.conf.bak backup first [root@localhost conf]# vim nginx.conf



Replace with, can support Chinese


Edit this section in the original file to indicate that the file name is test

[External chain picture transfer failed, source station may have anti-theft chain mechanism, it is recommended to save the picture and upload it directly (img-k4dsPoN5-1636356183329).) https://img-blg.csdnimg.cn/bf82b6b276f849508a94e8823a399fe1 )]

After wq is saved, check with the command that there is no problem before starting the service

[root@localhost conf]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@localhost conf]# service nginx start

You can see the home page.

Log in to test.html

Access State Statistics Configuration

Add this paragraph to the file

Restart service after editing confirmation

[root@localhost conf]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@localhost conf]# service nginx restart

Access on the Web


Active connections: 2
#Current number of active links
server accepts handled requests
2 2 6
#indicates the connection information that has been processed, and three numbers represent the number of connections processed, the number of successful TCP handshakes, and the number of requests processed at one time
Reading: 0 Writing: 1 Waiting: 1
Command Line View

[root@localhost conf]# curl 192.168.133.100/status Active connections: 1 server accepts handled requests 3 3 7 Reading: 0 Writing: 1 Waiting: 0
[root@localhost conf]# vim visit.sh

Access Control Based on Authorized Password

Generate user password authentication file

[root@localhost conf]# yum install httpd-tools -y [root@localhost conf]# Htpasswd-c/usr/local/nginx/passwd.db Zpq Create a new user and set the password, -c determines if there is this file New password: Re-type new password: Adding password for user zpq [root@localhost conf]# cat /usr/local/nginx/passwd.db zpq:$apr1$0..D5jHe$XrlL/8Rhpi5o95ipcaSY2.
[root@localhost conf]#vim /usr/local/nginx/conf/nginx.conf

Edit this paragraph

Check to confirm that the service restarted correctly

[root@localhost conf]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@localhost conf]# service nginx restart


Enter your password and you're ready to go to the home page

Client-based access control


Restricted host is inaccessible after restriction

The host can be accessed when the restriction is removed.

Domain name-based nginx virtual host

Create two file directories

[root@localhost ~]# mkdir -p /var/www/html/ [root@localhost ~]# ls /var/www/html/ fxy zy [root@localhost ~]# echo "This is zy's web" > /var/www/html/zy/index.html [root@localhost ~]# echo "This is fxy's web" > /var/www/html/fxy/index.html [root@localhost ~]# cat /var/www/html/fxy/index.html This is fxy's web [root@localhost ~]# cat /var/www/html/zy/index.html This is zy's web
[root@localhost conf]#vim /usr/local/nginx/conf/nginx.conf


Change to

Write the same two paragraphs one "zy" and one "fxy", because the paragraph is long, all copies are in front of the text

[root@localhost conf]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@localhost conf]# service nginx restart

Edit hosts file on client

Open the corresponding web address on the web page

IP Address Based

[root@localhost conf]#vim /usr/local/nginx/conf/nginx.conf

Increase the corresponding address to increase the virtual network card

[root@localhost conf]# ifconfig ens33:0 192.168.133.98 [root@localhost conf]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@localhost conf]# service nginx restart

Port-based

[root@localhost conf]#vim /usr/local/nginx/conf/nginx.conf

Change 2 local ip address ports

[root@localhost conf]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@localhost conf]# service nginx restart

8 November 2021, 15:58 | Views: 8355

Add new comment

For adding a comment, please log in
or create account

0 comments