Demonstration of the most basic configuration of nginx

Reverse proxy

Reverse proxy diagram

Configuration file nginx conf

Just configure the proxy in the server_ Pass is OK. Everything else is the default.

#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;
    
   # upstream myserver {
    #  server 222.182.202.16:8056;
     # server 222.182.202.16:8056;
   # }
    
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://127.0.0.1:8080/log-test/;
            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;
    #    }
    #}

}

Configuration interpretation

server_name can be configured as ip address (localhost by default) or domain name:
Only one ip can be configured for the same listen, but multiple domain names can be configured (one ip can be directed by multiple domain names)

server {
        listen       80;
        server_name  47.97.156.184; 

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://127.0.0.1:8080/log-test/;
            root   html;
            index  index.html index.htm;
        }
		....

server {
        listen       80;
        server_name  xl.domain.com;  #Point to 47.97.156.184

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://127.0.0.1:8080/log-test/;
            root   html;
            index  index.html index.htm;
        }
		....
		
		
server {
        listen       80;
        server_name  yy.domain.com; #It also points to 47.97.156.184

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://127.0.0.1:8080/log-test/;
            root   html;
            index  index.html index.htm;
        }
		....		

Where, the proxy under location_ Pass configuration rule: as long as it is an accessible address.

example

Because there is only one ECs, both nginx and tomcat are installed on this server, and there is no domain name, so you can directly use the ip address for configuration, but you can't demonstrate how to configure multiple servers with different domain names{
listen 80;
server_name
...
}
So only one server is configured

Install tomcat on the server

  • Port listening 8080
  • Deploy the test project log test and access the test

nginx.conf configuration

Start tomcat and nginx

Browser input nginx Conf configured server_name + listen. Because listen is 80, it can be omitted

conclusion

nginx configures the server_name + listen successfully proxy the server project proxy_pass

load balancing

Distribute the load (request) evenly (which can be adjusted through policy configuration) to multiple servers

to configure

  • Configure the list of servers to be allocated inside the http block and outside the server block, in the form of IP (domain name): port
  • Configure the name of the server list myserver in the server block

prepare

Prepare 8080 and 8081, directly access the test (do not go to nginx) and see the effect

Test load balancing through nginx

Access the configured server_name and listen:

  • First visit
  • Refresh access again
  • The configured nginx plays the above role

Dynamic and static separation

This is only a demonstration of configuring static resources

It can be a resource on the nginx local server

Configure server - > location

There is a corresponding directory on the service


The browser accesses the static resource file "how tomcat works.pdf"

The access is successful. This is the same as the virtual path configured by tomcat. See: tomcat configuration virtual path

Accessing static resources on other servers


Browser access:

summary

nginx has three major functions: reverse proxy, load balancing and dynamic static separation. The above shows only the most basic part. There are still many configurations and functions to be learned later.

Keywords: Web Server Linux Operation & Maintenance Nginx

Added by noimad1 on Sun, 30 Jan 2022 09:16:55 +0200