Dynamic and static separation of Nginx under linux CentOS7

Keywords: Operation & Maintenance Nginx Tomcat PHP Apache

The separation of static and dynamic doesn't explain. There are a lot of theories on the Internet, but the implementation means I understand is that specific requests use specific processing methods.

For nginx installation, please refer to this:

https://blog.csdn.net/tangyaliang11/article/details/78675535

For tomcat installation, please refer to this:
https://blog.csdn.net/tangyaliang11/article/details/81124349

For demonstration purposes only, take the picture as an example.
First, cut the. png image under the root folder of tomcat to the html under nginx.
At this time, you should not see the picture when visiting tomcat. If you can see it, please clean up the browser cache.

Then configure nginx.

There are three main configurations.
1. Add proxy to http node
 upstream drugs{
Server 192.168.2.4:8080; Tomcat address
    }
2. Add tomcat agent and image resolution directory at the server
Agent tomcat
        location / {
            proxy_pass http://drugs;
        }
Picture resolution directory
      location ~ .*\.(png|jpg)$ {
            root html;
        break;
        }
3. Open the log comment. (it's important to read the log)
 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;

4. If the operation is successful, check the log. If there is the keyword "Permission denied" in the log, you have no permission to read the picture. Uncomment the "user root" of nginx.conf, and reload is trying to access ok.
The following is the full configuration of nginx. The jsp page of tomcat does not need to be modified.

user  root;
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 drugs{
	  server 192.168.2.4:8080;
	}
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
	  
        location / {
            proxy_pass http://drugs;
        }
      location ~ .*\.(png|jpg)$ {
            root /usr/local/nginx/html;
		break;
        }

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

}

 

Posted by jh21236 on Wed, 11 Dec 2019 12:25:50 -0800