1) Of course you need to download the software you want to use. Go to the nginx official website for the next one. http://nginx.org/en/download.html can be found here. The version I am using now is 1.1.7, but basically all future versions are compatible, and what we are using does not involve too low-level aspects, so there should be no changes. Here, since mine is Windows, of course I download the Windows version. After downloading, you need to start it first. Enter the nginx folder and start nginx directly. For example, after downloading, I put it in d:\software\developertools\server\nginx-1.1.7, directly cmd and then cd d:\software\developertools\server\nginx-1.1.7. Some people who are not used to the command line may be surprised. It doesn't proceed to that folder. Windows will not jump between partitions unless you specify it yourself. So we have to directly d: as follows:
Then, we start nginx directly. Here you may see a window flashing by. According to our use of tomcat If the experience passes by in a flash, it proves that something is wrong, right? But it's not.
#At this time we open the task manager and can see two nginx.exe there. This shows that we have already started. As for why, we will not delve into it here.
Now that we have started nginx, we can start tomcat. If we want to directly access http://localhost, we can directly access tomcat. Don't worry, let's take a look at what nginx looks like after starting. Directly accessing http://localhost can see:
We can see that nginx started successfully, and now the access is directly into the nginx directory. So where are these actually configured. This involves nginx.conf, an important configuration file of nginx.
2) We can see that there is a conf folder in the nginx folder, which contains several files. Regardless of the others, we open nginx.conf and we can see a paragraph:
This code is in the server and is equivalent to a proxy server. Of course, multiple ones can be configured. Let's analyze it carefully: listen: Indicates the port that the current proxy server listens to. The default is to listen to port 80. Note that if we configure multiple servers, the listener must be configured differently, otherwise we will not be able to determine where to go. server_name: Indicates where to go after listening. At this time, we go directly to the local area. At this time, it is directly to the nginx folder. location: Indicates the matching path. If / is configured, it means that all requests will be matched here. Root: If root is configured, it means that when the path of this request is matched, the corresponding file will be found in this folder. Here It will be useful for our later static file serving. index: When no home page is specified, the specified file will be selected by default. There can be multiple files and they will be loaded in order. If the first one does not exist, the second one will be found, and so on. The error_page below is the page that represents the error. We will not use it here for the time being, so we will ignore it for now.
Now that we know the specific configuration, how to make it switch to tomcat when accessing localhost. In fact, only two places have been modified:
server_name localhost:8080; location / { proxy_pass http://localhost:8080}
We have modified the above two places. My tomcat is on port 8080, which can be modified according to your own needs. There is a new element proxy_pass here, which represents the proxy path, which is equivalent to forwarding, unlike the root that must be specified before. Now that we have modified the file, does it mean that we must shut down nginx and then restart it? In fact, it is not necessary, nginx can reload the file. We run directly: nginx -s reload
We were too happy, we found an error:
What did it come from, the error was found on line 45, We didn’t want to find } in that line, so we looked carefully and found that the proxy_pass we added was very strange. It didn’t end with a ; sign. This was the problem. We modified it directly and then ran it again. We found that there was no error and it was ok. If you don't want to load it directly, but just want to see if there are any problems with your configuration file, you can directly enter: nginx -t
This can check whether there are errors in the configuration file. All our modifications below assume that we run nginx -s reload to reload the configuration file after the modification is completed. Please note.
Everything is fine, then we reopen http://localhost and we see the following page:
这时,我们发现它并不是刚才的welcome页面了,而是tomcat的管理页面了,不管我们点击什么链接都是没问题的,相当于直接访问http://localhost:8080一样。
3)上面我们直接试了一个小例子,让nginx进行转发,即所谓的反向代理。但实际上我们的需求不会是这样的,我们需要分文件类型来进行过滤,比如jsp直接给tomcat处理,因为nginx并不是servlet容器,没办法处理jsp,而html,js,css这些不需要处理的,直接给nginx进行缓存。下面我们来进行一下配置,让jsp页面直接给tomcat,而html,png等一些图片和js等直接给nginx进行缓存。这时最主要用的还是location这个元素,并且涉及到一部分正则,但不难:
location ~ .jsp$ { proxy_pass http://localhost:8080;} location ~ .(html|js|css|png|gif)$ { root d:/software/developertools/server/apache-tomcat-7.0.8/webapps/root;} location ~ .jsp$ { proxy_pass http://localhost:8080;} location ~ .(html|js|css|png|gif)$ { root d:/software/developertools/server/apache-tomcat-7.0.8/webapps/root;}
我们先要去掉之前配的location /,避免全部请求被拦截了。然后我们再来看看http://localhost
当我们不指定jsp页面的时候,它会出现找不到,因为,此时并没有相应的location匹配,所以就会有404错误,这时就跳到了nginx自定义的error页面去了。而当我们用http://localhost/index.jsp去访问时,我们看到了熟悉的页面:
而且图片那些都显示正常,因为图片是png的,所以直接在tomcat/webapps/root目录下直接查找,当然,如果我们点击manager application how-to这个链接,我们发现:
它还是找不到,为什么呢?因为这是个html页面,但它并不在root目录下,而是在docs目录下,但当我们匹配html时,我们却到root目录下去找,所以还是找不到这个页面。
一般情况下,如果我们需要用nginx来进行静态文件伺服,一般都会把所有静态文件,html,htm,js,css等都放在同一个文件夹下,这样就不会有tomcat这样的情况了,因为tomcat下的是属于不同的项目,这个我们就没办法了。
3)有些人会说,这些都只会找一台服务器,但如果我们想在一台服务器挂了的时候,自动去找另外一台,这怎么办?这实际上nginx都考虑到了。这时,我们之前用的proxy_pass就有大用途了。我们把之前的第一个例子,即全部都代理的修改一下:最后修改如下:
upstream local_tomcat { server localhost:8080;} server{ location / { proxy_pass http://local_tomcat; } ##......其他省略 } upstream local_tomcat { server localhost:8080;} server{ location / { proxy_pass http://local_tomcat; } #......其他省略 }
我们在server外添加了一个upstream,而直接在proxy_pass里面直接用http://+upstream的名称来使用。我们还是直接来http://localhost,还是和第一个一样的效果,所有链接都没问题,说明我们配置正确。upstream中的server元素必须要注意,不能加http://,但proxy_pass中必须加。我们刚才说可以在一个服务器挂了的情况下连到另外一个,那怎么弄呢?其实很简单,在upstream中的local_tomcat中配置多一个server。比如我现在弄多一个jetty,端口在9999,所以我们配置如下:
upstream local_tomcat { server localhost:8080; server localhost:9999;} upstream local_tomcat { server localhost:8080; server localhost:9999;}
此时,我们关闭tomcat,而只开jetty。我们来运行http://localhost看看效果: 我们看到它请求到了jetty的页面,但由于jetty的机制,这时没有显示jetty主页,这个我们先不管。但我们的在一个服务器挂的情况下自动使用另外一个的功能实现了。
但有时我们就不想它挂的时候访问另外一个,而只是希望一个服务器访问的机会比另外一个大,这个可以在server最后加上一个weight=数字来指定,数字越大,表明请求到的机会越大。
upstream local_tomcat { server localhost:8080 weight=1; server localhost:9999 weight=5;} upstream local_tomcat { server localhost:8080 weight=1; server localhost:9999 weight=5;}
这时我们给了jetty一个更高的权值,让它更有机会访问到,实际上当我们刷新http://localhost访问的时候发现jetty访问机率大很多,tomcat几乎没机会访问,一般情况下,如果我们必须这样用,不要相关太大,以免一个服务器负载太大。当然,server还有一些其他的元素,比如down表示暂时不用到该服务器等等。
The above is the detailed content of How to use Nginx as reverse proxy for Tomcat server. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于nginx的相关知识,其中主要介绍了nginx拦截爬虫相关的,感兴趣的朋友下面一起来看一下吧,希望对大家有帮助。

高并发系统有三把利器:缓存、降级和限流;限流的目的是通过对并发访问/请求进行限速来保护系统,一旦达到限制速率则可以拒绝服务(定向到错误页)、排队等待(秒杀)、降级(返回兜底数据或默认数据);高并发系统常见的限流有:限制总并发数(数据库连接池)、限制瞬时并发数(如nginx的limit_conn模块,用来限制瞬时并发连接数)、限制时间窗口内的平均速率(nginx的limit_req模块,用来限制每秒的平均速率);另外还可以根据网络连接数、网络流量、cpu或内存负载等来限流。1.限流算法最简单粗暴的

nginx php403错误的解决办法:1、修改文件权限或开启selinux;2、修改php-fpm.conf,加入需要的文件扩展名;3、修改php.ini内容为“cgi.fix_pathinfo = 0”;4、重启php-fpm即可。

实验环境前端nginx:ip192.168.6.242,对后端的wordpress网站做反向代理实现复杂均衡后端nginx:ip192.168.6.36,192.168.6.205都部署wordpress,并使用相同的数据库1、在后端的两个wordpress上配置rsync+inotify,两服务器都开启rsync服务,并且通过inotify分别向对方同步数据下面配置192.168.6.205这台服务器vim/etc/rsyncd.confuid=nginxgid=nginxport=873ho

跨域是开发中经常会遇到的一个场景,也是面试中经常会讨论的一个问题。掌握常见的跨域解决方案及其背后的原理,不仅可以提高我们的开发效率,还能在面试中表现的更加

nginx部署react刷新404的解决办法:1、修改Nginx配置为“server {listen 80;server_name https://www.xxx.com;location / {root xxx;index index.html index.htm;...}”;2、刷新路由,按当前路径去nginx加载页面即可。

nginx禁止访问php的方法:1、配置nginx,禁止解析指定目录下的指定程序;2、将“location ~^/images/.*\.(php|php5|sh|pl|py)${deny all...}”语句放置在server标签内即可。

linux版本:64位centos6.4nginx版本:nginx1.8.0php版本:php5.5.28&php5.4.44注意假如php5.5是主版本已经安装在/usr/local/php目录下,那么再安装其他版本的php再指定不同安装目录即可。安装php#wgethttp://cn2.php.net/get/php-5.4.44.tar.gz/from/this/mirror#tarzxvfphp-5.4.44.tar.gz#cdphp-5.4.44#./configure--pr


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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver CS6
Visual web development tools

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