2021-09-08-Nginx Note 1: Nginx Basic Configuration

Forward Proxy Server

  1. The server between the client and the target server. The client sends a request to the proxy and specifies the target server. Then the proxy requests and obtains the content from the target server and returns it to the client. In general, the proxy server is a forward proxy server.
  2. Core: Users know the target server they are accessing
  3. Scene: a springboard, visiting websites that were previously inaccessible, such as some foreign sites

Reverse Proxy Server

  1. The server between the client and the target server. The client sends a request to the proxy, then the proxy requests and gets the content from the target server and returns it to the client.The reverse proxy hides the real server
  2. Core: The client does not know which server the target server is accessing, and the proxy will select a real server for requests based on certain policies
  3. Scenario: Visit Taobao and know that the domain name you are visiting is taobao.com, but the specific server you are visiting is not known to you

Centos7 Quick Install Nginx Compiled Version

  1. Download the installation package: http://nginx.org/en/download.html
  2. Installation Dependency:
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
  1. Create a folder to upload locally provided nginx packages
//Execute Command
./configure
make
make install 
  1. Default installation path
/usr/local/nginx
  1. Access Configuration
cd /usr/local/nginx/sbin   
./nginx

Domain Name Recording and Online Resolution Server

  1. Basic flow of an http request

Clients Access Web Sites by Domain Name -- "DNS Resolves Domain Name to Get IP --> Find Server to Get Resources

  • DNS servers are used to resolve domain names
  1. DNS Resolve Record
    • a Record

Users can set up a domain name here to resolve directly to the target host, also known as an ip-to-domain name resolution server

  • CNAME

Alias pointing, you can set an alias for the server, such as aa.com pointing to bb.com, then bb.com can be accessed through aa.com in the future
Individuals think aliases are also domain names and need to purchase them

Nginx Directory Understanding

  1. HTML directory: contains default page index.html
  2. logs directory: access.log access log, error.log error log, nginx.pid process PID

Nginx related commands

  1. Reload Configuration File
./nginx -s reload
  1. Specify profile loading
./nginx -c xxxx.conf
  1. Stop nginx
./nginx -s stop
  1. Force Kill Nginx
ps -ef |grep nginx
kill -9 master-pid

Nginx Configuration Understanding

  1. View nginx.conf
#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;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            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;
        #}
    }


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

}

  1. Global Configuration
#user  nobody;   # Enable Account
worker_processes  1;   #Work process (as consistent as possible with the number of cpu cores)

#error_log  logs/error.log;   # Error log storage path
#error_log  logs/error.log  notice;  #Error Log Open
#error_log  logs/error.log  info;# log level

#pid        logs/nginx.pid;    # Started process id


events {
    use epoll;   # Blocking model, add epoll, asynchronous non-blocking*added
    worker_connections  1024;  #Maximum number of connections per process, if a request has a backend, consumes 2 connections, 768/2, or 768 if no backend exists
}

  1. Application Configuration
http {
    include       mime.types;   #Media type params.JSON,body,formdata,x-www-form-urlencoded, etc.
    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'';This is the log corresponding template information, you can see the corresponding parameter data in the request log access.log

    #access_log  logs/access.log  main;  #Corresponding log storage path and template main used

    sendfile        on;  #Turn on efficient transmission mode
    #tcp_nopush     on;  # Reduce network message size and improve performance

    #keepalive_timeout  0;
    keepalive_timeout  65; # Client connection timeout

    #gzip  on;  # Compression of transmitted data to save bandwidth
  	 server {
        listen       80;  #Listening Port
        server_name  localhost;# Configure the domain name, you can configure multiple domain names, separated by spaces

        #charset koi8-r;

        #access_log  logs/host.access.log  main;# Each virtual host can configure its own access log

        location / {     # Configure access routes to configure multiple routes
            root   html;  # Indicates that the storage path html is a relative path or that an absolute path can be configured
            index  index.html index.htm;# Find files in this path
        }

        #error_page  404              /404.html;

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

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ { # Configure the route at the end of.php
        #    proxy_pass   http://127.0.0.1; #Configure Forwarding Address
        #}

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

}

Use Nginx to access the front end

  1. Using nginx to access the front end
server {
        listen       8820;
        server_name  192.168.0.129;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /front/dict/;
            index  index.html;
        }
}

Use Nginx as a picture server

  1. Use Nginx to access pictures
server {
        listen       8820;
        server_name  192.168.0.129;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /abc {
          alias /project/app/js/; #Last but not least, add a slash
        }
}

1. Configure the root directory in location /
2. Configure the alias virtual directory in location/path with the'/'symbol behind the directory

  1. Second Writing
server {
        listen       8820;
        server_name  192.168.0.129;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /js/ {   # Notice here that with the last / js / backslash added, nginx will complete / js / behind the absolute path
          root /project/app; #You can't add a backslash here at the end, the location will be appended to the back, and the picture will be uploaded to / project/app/js/directory
          autoindex on;
        }
}

It can be understood as follows:
1. Backslashes are added to routes following locations, and roots cannot add backslashes in the end.Because nginx adds backslashes in locationh as routes after root/project/app/js/
2. location does not add backslashes, root does not append url/js/
3. The specific reasons are not understood

Keywords: Operation & Maintenance Nginx memcached

Added by rileyrichter on Wed, 08 Sep 2021 08:12:56 +0300