


nginx reverse proxy, separation of dynamic and static requests, and nginx cache application, and use ngx_cache_purge to clear the specified URL
一,nginx反向代理配置
#tomcat
Java代码
- upstream tomcat_server{
- server 127.0.0.1:8080;
- }
- erver{
- listen 80;
- server_name www.codes51.com;
- location / {
- proxy_redirect off;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_pass http://tomcat_server;
- }
显然就是用户访问www.codes51.com(需要设置本地localhost,将www.codes51.com指向nginx所在IP)的时候(或将www.codes51.com直接写在nginx所在的IP地址),将请求转到到后台的tomcat服务器,即127.0.0.1:8080,并将请求到的数据转发给client
二,动静态请求相分离
神马意思?图片,JS,HTML等静态的东西去访问一台专门的服务器,而动态的请求去访问另一台服务器。就这么简单,上例子:
Java代码
- server {
- listen 192.168.154.128:80;
- server_name image.codes51.com;
- index index.html;
- #proxy_pass http://tomcat_server;
- #charset koi8-r;
- #access_log logs/host.access.log main;
- location / {
- root html;
- #index index.html index.htm;
- proxy_redirect off;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- }
- location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
- {
- valid_referers none blocked 192.168.154.128 192.168.154.1;
- if ($invalid_referer)
- {
- rewrite ^ /403.jpg break;
- }
- if (!-f $request_filename) {
- rewrite ^ /404.jpg last;
- }
- expires 30d;
- }
- #error_page 404 /404.html;
- # redirect server error pages to the static page /50x.html
- #
- error_page 500 502 503 504 /50x.html;
- location = /404.jpg {
- root html;
- }
- }
- tomcat
- upstream tomcat_server{
- server 127.0.0.1:8080;
- }
- server{
- listen 192.168.154.128;
- server_name www.codes51.com;
- location / {
- proxy_redirect off;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_pass http://tomcat_server;
- }
- }
上面这种方式是通过设定不同的域名,可不可以在同一个域名中,通过判断后缀来将动态与静态请求相分离呢?
Java代码
- #tomcat
- upstream tomcat_server{
- server 127.0.0.1:8080;
- }
- server{
- listen 192.168.154.128;
- server_name www.codes51.com;
- location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
- {
- root html;
- }
- location ~ .*.(jsp|do)$ {
- proxy_redirect off;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_pass http://tomcat_server;
- }
OK!同一个域名,根据后缀不同,请求不同的服务,实现动态静态请求相分离。仔细想一想,如果又出现了一种静态的文件,比如*.abc ,那难道又去修改那个配制文件吗?显然不太合理,所以可以考虑将所有的表态文件放在同一个根目录下面,比如/static那么可以将上面的静态页面请求修改一下:
Java代码
- location /static
- {
- root html/static;
- }
咦这样是不是就好一些了,而且文件的存放也比较有规范了。
三,nginx缓存应用
nginx具有web缓存服务,proxy_cache,但是有一个问题就是,proxy_cache不能清除指定的URL缓存,只能设置URL过期时间,但是有问题,有人就会很快解决问题,nginx第三方模块ngx_cache_purge能清除指定URL。
nginx安装时需要将ngx_cache_purege加载进去。
Java代码
- ./configure --user=www --group=www --add-module=/root/dxm/nginx/ngx_cache_purge-1.2
其中,/root/dxm/nginx/ngx_cache_purge-1.2为ngx_cache_purge解压路径(附件中提供ngx_cache_purge tar包下载)
现在来一段实例,实现图片缓存:
By the way, proxy_tem_path and proxy_cache_path must be under the same partition!
Java code
- proxy_temp_path /usr/local/nginx/proxy_temp;
- proxy_cache_path /usr/local/nginx/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=1g;
Java code
- upstream tomcat_server{
- server 127.0.0.1:8080;
- }
- server{
- listen 192.168.154.128;
- server_name www.codes51.com;
- location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
- {
- proxy_cache cache_one;
- proxy_cache_methods GET HEAD POST;
- proxy_cache_min_uses 1;
- proxy_cache_valid 200 302 10m;
- proxy_cache_valid 404 1m;
- proxy_cache_valid any 1m;
- proxy_cache_key "$host:$server_port$uri$is_args$args";
- proxy_redirect off;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_pass http://tomcat_server;
- }
- location ~ .*.(jsp)$ {
- proxy_redirect off;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_pass http://tomcat_server;
- }
- location ~ /purge(/.*)
- Allow 192.168.154.128;
- Allow 192.168.154.1;
- deny all; proxy_cache_purge cache_one $host:$server_port$
- 1$is_args$args; }
-
Well, static pages are cached, but dynamic requests are not cached!
Take a look at the purege configuration in the last paragraph. Obviously, it indicates which IPs can manually clear the specified URL.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

To deploy a JAR project to Tomcat, follow these steps: Download and unzip Tomcat. Configure the server.xml file, set the port and project deployment path. Copies the JAR file to the specified deployment path. Start Tomcat. Access the deployed project using the provided URL.

To allow the Tomcat server to access the external network, you need to: modify the Tomcat configuration file to allow external connections. Add a firewall rule to allow access to the Tomcat server port. Create a DNS record pointing the domain name to the Tomcat server public IP. Optional: Use a reverse proxy to improve security and performance. Optional: Set up HTTPS for increased security.

To deploy multiple projects through Tomcat, you need to create a webapp directory for each project and then: Automatic deployment: Place the webapp directory in Tomcat's webapps directory. Manual deployment: Manually deploy the project in Tomcat's manager application. Once the project is deployed, it can be accessed by its deployment name, for example: http://localhost:8080/project1.

Tomcat installation directory: Default path: Windows: C:\Program Files\Apache Software Foundation\Tomcat 9.0macOS:/Library/Tomcat/Tomcat 9.0Linux:/opt/tomcat/tomcat9 Custom path: You can specify it during installation. Find the installation directory: use whereis or locate command.

The Tomcat website root directory is located in Tomcat's webapps subdirectory and is used to store web application files, static resources, and the WEB-INF directory; it can be found by looking for the docBase attribute in the Tomcat configuration file.

How to check the number of concurrent Tomcat connections: Visit the Tomcat Manager page (http://localhost:8080/manager/html) and enter your user name and password. Click Status->Sessions in the left navigation bar to see the number of concurrent connections at the top of the page.

The Tomcat port number can be viewed by checking the port attribute of the <Connector> element in the server.xml file. Visit the Tomcat management interface (http://localhost:8080/manager/html) and view the "Status" tab. Run "catalina.sh version" from the command line and look at the "Port:" line.

Running projects with different port numbers on the Tomcat server requires the following steps: Modify the server.xml file and add a Connector element to define the port number. Add a Context element to define the application associated with the port number. Create a WAR file and deploy it to the corresponding directory (webapps or webapps/ROOT). Restart Tomcat to apply changes.
