Introduction to the

Nginx is HTTP and reverse proxy server, mail proxy server, and generic TCP/UDP proxy server. In short, nginx is hot and awesome.

download

Download: nginx.org/en/download… ; Mainline: latest version stable: stable version

Wget [下载 address] tar -zxvfCopy the code

catalogue

Conf: example file configure: used to generate intermediate files that perform required actions before compilation. Contrib: Vim contrib/vim/* ~/.vim/ HTML: : nginx default HTML file man: nginx help file SRC: nginx source code

The installation

.configure --xxx

–with-xxx –with-xxx –with-xxx –with-xxx –with-xxx –with-xxx –with-xxx –with-xxx –with-xxx –with-xxx –with-xxx –with-xxx –with-xxx –with-xxx –with-xxx –with-xxx –with-xxx –with-xxx –with-xxx –with-xxx –with-xxx –with-xxx –with-xxx –with-xxx

Configure –prefix=/usr/local/nginx generates an objs directory containing intermediate files. The objs/ngx_modules.c file determines which modules are installed on nginx.

Make Generates a large number of intermediate files and stores them in objs/ SRC. Install: make install

Nginx command line

-c Specifies the configuration file -g specifies the configuration command -p Specifies the running directory -s stop Stops the service immediately. Quit gracefully stops service; Reload reload configuration file; The reopen reopen records the log file. -t Tests whether the syntax of the configuration file is incorrect. -v version information is displayed

Reloading configuration files

After modifying the contents of the nginx configuration file, nginx needs to reload the configuration file

nginx -s reload
Copy the code

Hot deployment

When nginx is running, you need to upgrade the nginx version. You just need to update the nginx binaries. Back up old nginx:

cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
Copy the code

Replace the nginx binary in use with the latest nginx binary.

cp -r nginx /usr/local/nginx/sbin/ -f
Copy the code

View the running nginx master process

ps -ef | grep nginx
Copy the code

Tell the master running Nginx that an Nginx upgrade is required

kill-usr2 [ID of the running nginx master process]Copy the code

After executing the command, a new nginx process is started and the old nginx master process is told to gracefully shut down all old worker processes

kill-winch [old nginx master process ID]Copy the code

You will then find that the old Nginx worker process has been shut down and the master process is still in place. If an error occurs in the new version of nginx, you can go back to the old nginx master process and perform nginx -s reload to revert to the old version

Log cutting

Back up the log files first
mv access.log access_bak.log

The access. Log file will be regenerated after executing the command
nginx -s reopen
Copy the code

In general, a bash script is created after the session to periodically cut logs.

Static resource server

Nginx configuration

Main specifies the name of the log format
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                   '$status $body_bytes_sent "$http_referer" '
                   '"$http_user_agent" "$http_x_forwarded_for"'; 
	
gzip on; Zip file transfer
gzip_min_length 1; # Less than 1 byte is not compressed.
gzip_comp_level 2; # Compression level
gzip_types image/jpeg image/gif; These files are compressed

server {
    listen 80; # monitor port
    server_name localhost; # the domain name
	
	Log access_log (every request is logged) using main's log_format
	access_log /logs/blog.log main; 

    The url matches the path
    location / { 
	    alias  code/; Nginx installation directory eg: /usr/local/nginx/code
	    # autoindex on; Share static resources
	    # set $limit_rate 10k; # nginx transmits 10K bytes per second to the browser}}Copy the code

Reverse proxy service with caching function

Multiple upstream services can be set up, and when a request comes in, Nginx can delegate to multiple upstream servers based on load balancing algorithms. Nginx configuration

# Upstream service
# local indicates the upstream server name
upstream local {
     # One upstream server, multiple servers can be configured
	 # 127.0.0.1:8080 indicates that only this function can access port 8080Server 127.0.0.1:8080; }# reverse proxy cache cache path memory keyword, 10m
proxy_cache_path /tmp/nginxcache levels=1:2 keys_zone=my_cache:10m max_size=10g;
inactive=60m use_temp_path=off;

server {
	listen 80;
    server_name colablog.cn; # the domain name
	
	Log access_log (every request is logged) using main's log_format
	access_log /logs/blog.log main; 

    The url matches the path
    location / { 
	    # doc http://nginx.org/en/docs/http/ngx_http_proxy_module.html
	    # proxy_set_header The reverse proxy server sets the information requested by the client to the request header and sends it to the upstream service
	    proxy_set_header Host $host; # the domain name
		proxy_set_header X-Real-IP $remote_addr; # client address
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		
		Which cache to use corresponds to the keys_zone above
		proxy_cache my_cache;
		The cache path
		proxy_cache_key $hots$uri$is_args$args;
		These responses are not cached
		proxy_cache_valid 200 304 302 1d;
	
	    Proxy to upstream service
	    proxy_pass http://local; }}Copy the code

GoAccess Visualizes real-time monitoring of Access logs

The installation

You can use apt Install or yum Install as a quick way to install apt install. Run the goaccess command

# goaccess /usr/local/nginx/logs/access.log -o /usr/local/nginx/html/report.html --real-time-html --time=format='%H:%M:%S' --date-format='%d/%b/%Y' --log-format=COMBINED
Copy the code

–real-time HTML stands for updating pages in real time

Nginx. conf configuration file

    # log format
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

    server {
        listen 80;
        server_name: localhost;
        # log
        access_log logs/access.log main;
        
        # specify page
        location /report.html {
            alias /usr/local/nginx/html/report.html; }}Copy the code

Then go to http://localhost/report.html, you can see below so tall on the interface.

SSL

If you have a domain name, just two lines of command can quickly change your http:// domain name to https://domain name. Under the ubuntu version

apt install python-certbot-nginx
Copy the code

Under the centos version

yum install python2-certbot-nginx
Copy the code

Use the certbot command to help us download the certificate and automatically configure nginx.conf,

certbot --nginx --nginx-server-root=/usr/local/nginx/conf/ -d[Your domain name]Copy the code

After executing the above command, there are two options. The first option is to access HTTP or HTTPS without redirection. The second option is to redirect to HTTPS when accessing HTTP. That’s it. That’s easy, isn’t it?

conclusion

Nginx for the first time the small white, if there are mistakes in the article, please point out.

Reference article: Geek Time: Nginx core knowledge 100 lectures

Personal blog: colablog.cn/

If my article helps you, you can follow my wechat official number and share the article with you as soon as possible