抄自

https://www.cnblogs.com/j-star/p/8785334.html

个人理解

nginx端口设置为80,简称n

tomcat端口设置为其他,例如8080,简称t

php网站和javaweb网站域名解析到服务器上面

当访问java域名时,n判断为java网站,就跳转到tomcat

当访问php域名时,n判断为php网站,就不跳转了

n就是个快递员

http中的server可以添加多个,每一个就是一个判断吧(大概)

下面是我的一个n的配置文件,第一个server是自带,第二个是判断javaseb的

#  power by www.phpStudy.net
#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;
#tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 128k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k; #gzip on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 32k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
gzip_disable "MSIE [1-6]."; server_names_hash_bucket_size 128;
client_max_body_size 100m;
client_header_buffer_size 256k;
large_client_header_buffers 4 256k; server {
listen 80;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main;
root "C:/php/XXX/WWW/test";
location / {
index index.php index.html index.htm l.php;
autoindex off;
} #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 "C:/php/XXX/WWW/test";
} # 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(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
server{
listen 80;
server_name www.XXX.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://www.XXX.com:8080;
index index.html index.htm;
}
} # 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;
# server_name localhost; # ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key; # ssl_session_timeout 5m; # ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on; # location / {
# root html;
# index index.html index.htm;
# }
#} include vhosts.conf; }

以下是摘抄的内容

Nginx是一款轻量级Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强。

下面就详细的说一说如何用Nginx实现反向代理。

首先:来做点准备工作,最少两个tomcat,另外设置两个域名并解析到本地ip(因为nginx是直接配置域名)

(1)准备两个tomcat

分别设置两个tomcat的/conf/server.xml中的端口号,如下

tomcat1端口号分别设置为:8205    8280   8209

tomcat2端口号分别设置为:8215    8281   8219

(2)添加测试内容

分别删除目录webapps下所有文件,新建一个文件夹ROOT,并在该目录下新建index.html

内容可以设置为:这里是8280端口。(另一个:这里是8281端口。)

(3)启动两个tomcat

(4)分别访问两个tomcat下的项目

访问下面两个网址:

127.0.0.1/8280

127.0.0.1/8281

如果能够访问到对应的index.html文件就说明tomcat配置且启动成功。

(5)设置两个域名并做解析

修改hosts文件,目的是为了设置2个域名, tomcat1.com和 tomcat2.com并且解析到本地ip:127.0.0.1

路径:C:\Windows\System32\drivers\etc

打开hosts文件,加上(如果修改后无法保存,可以把hosts文件复制到桌面,修改完成后再替换原有hosts文件)

添加内容如下:

127.0.0.1 tomcat1.com
127.0.0.1 tomcat2.com

(6)测试域名是否解析成功

127.0.0.1/8280;127.0.0.1/8281分别修改为

tomcat1.com:8280;tomcat2.com:8281

如果显示结果如上图所示,说明域名设置并解析成功。

下面开始正式配置Nginx服务器的方向代理。

1. 下载、安装Nginx服务器

输入网址:http://nginx.org/en/download.html ,选个合适的版本,下载后解压到合适的路径。

解压后,如图所示:nginx.exe可以直接运行,但是不建议。

2. 配置反向代理

打开/conf/nainx.conf文件,删除server{ …… },或者注销掉

重新添加server{  }

listen:监听的端口号

server_name: 访问域名

location :这里配置为/   直接匹配端口下的默认ROOT下的项目,location内容很广泛,

参见:https://segmentfault.com/a/1190000009651161

proxy_pass  :为项目的实际访问地址

server {
listen 80;
server_name tomcat1.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://127.0.0.1:8280;
index index.html index.htm;
}
}
server {
listen 80;
server_name tomcat2.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://127.0.0.1:8281;
index index.html index.htm;
}
}

3.启动、关闭、重启Nginx服务器

打开cmd.exe运行命令。

注意:一定要在nginx.exe文件所在的文件夹下操作。

(1)启动:

H:\nginx-1.12.2>start nginx

或H:\nginx-1.12.2>nginx.exe

注:建议使用第一种,第二种会使你的cmd窗口一直处于执行中,不能进行其他命令操作。

(2)停止:

H:\nginx-1.12.2>nginx.exe -s stop

或H:\nginx-1.12.2>nginx.exe -s quit

注:stop是快速停止nginx,可能并不保存相关信息;quit是完整有序的停止nginx,并保存相关信息。

(3)重新载入Nginx:

H:\nginx-1.12.2>nginx.exe -s reload

当配置信息修改,需要重新载入这些配置时使用此命令。

(4)重新打开日志文件:

H:\nginx-1.12.2>nginx.exe -s reopen

4. 测试是否配置成功

输入域名:tomcat1.com和tomcat2.com

如果输入域名后显示如上图,即为成功配置。

5.可能会遇到的问题(进程关闭失败)

在使用的过程中,你可能会遇到一个很奇葩的问题,就是当关闭命令执行了无数遍,页面却还是可以直接用域名访问。

这个时候就说明你的进程并没有真正关闭,我们可以用命令来查询一下:

cmd中输入:netstat -an|find "0:80"

如果有,说明真的没有关闭这个进程。没有,那就看看是不是浏览器缓存的问题。

针对进程不能正常关闭,可以用如下命令来关闭:taskkill /F /IM nginx.exe > nul

以上就是用nginx服务器做反向代理服务器的全部内容,希望对你有所帮助。

windows版 nginx配置反向代理实例教程 跳转tomcat和php网站的更多相关文章

  1. nginx配置反向代理详细教程(windows版)

    内容属于原创,如果需要转载,还请注明地址:http://www.cnblogs.com/j-star/p/8785334.html Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(I ...

  2. Linux - Nginx配置反向代理。

    Nginx配置反向代理. 准备两台服务器 http://192.168.70.66 http://192.168.70.62 设置正则匹配(192.168.70.66) vim /usr/local/ ...

  3. Windos环境用Nginx配置反向代理和负载均衡

    Windos环境用Nginx配置反向代理和负载均衡 引言:在前后端分离架构下,难免会遇到跨域问题.目前的解决方案大致有JSONP,反向代理,CORS这三种方式.JSONP兼容性良好,最大的缺点是只支持 ...

  4. CentOS 7 安装 Nginx 配置反向代理

    Linux使用Nginx Yum存储库上安装Nginx,适用于Red Hat Enterprise Linux和CentOS系统. 1.添加设置Nginx Yum存储库 在CentOS中首次安装Ngi ...

  5. [笔记]nginx配置反向代理和负载均衡

    1.nginx配置文件:源码安装情况下,nginx.conf在解压后的安装包内.yum安装,一般情况下,一部分在/etc/nginx/nginx.conf中,一部分在/etc/nginx/conf.d ...

  6. nginx笔记 安装nginx 配置 反向代理 多虚拟主机

    1,检测linux上是否 通过yum安装了nginxrpm   -qi  nginx 2.安装nginx之前的依赖包yum install gcc patch libffi-devel python- ...

  7. nginx 配置反向代理,负载均衡实战解析

    前言:NGINX的反向代理和负载均衡是网站架构中经常用到的一种高并发,高可用的方案,下面我们直接实战操作,当然理论也是要的. 一.反向代理 过程:反向代理:客户端 一>代理 <一> ...

  8. nginx 配置反向代理和负载均衡

    Nginx的配置文件: nginx安装目录/conf/nginx.conf 重新加载配置文件 ./nginx -s reload 配置虚拟主机 一个server就是一台虚拟主机 server { li ...

  9. nginx 配置反向代理和静态资源

    https://unit.nginx.org/integration/ 与NGINX集成 在NGINX后面安装单元 将NGINX配置为静态Web服务器,并在Unit前面配置反向代理. NGINX直接从 ...

随机推荐

  1. wifidog用php实现验证流程

    原创性声明 此博文的出处 为 http://blog.csdn.net/zhujunxxxxx/article/details/25384909假设进行转载请注明出处.本文作者原创,邮箱zhujunx ...

  2. C++模板类代码只能写在头文件?

      这个问题,实际上我几年前就遇到了.最近写个模板类玩的时候,再次遇到.   当我非常仔细的将定义和实现分开,在头文件中保留了最少的依赖后,一切就绪.cpp单独编过.但是当使用的时候,就会报告所有的函 ...

  3. jquery20--animate() : 运动的方法

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  4. 洛谷P1439 最长公共子序列(LCS问题)

    题目描述 给出1-n的两个排列P1和P2,求它们的最长公共子序列. 输入输出格式 输入格式: 第一行是一个数n, 接下来两行,每行为n个数,为自然数1-n的一个排列. 输出格式: 一个数,即最长公共子 ...

  5. spark源码编译,本地调试

    1.下载源码 2.进入源码根据README.md编译源码,注意使用的是源码目录下的maven编译 3.用idea导入顶层pom文件 4.修改顶层pom文件和example下的pom文件,将scope的 ...

  6. Java中Webservice调用.NET天气接口生成客户端异常

    学习webservice时候有个例子调用公网的天气预报接口实现查询天气的功能.然而在使用命令编译客户端代码的时候出错了.大概看了一下网上说是需要将将文件中所有出现的 < s:element re ...

  7. PHP防止Xss攻击

    mysql_real_escape_string() 所以得SQL语句如果有类似这样的写法:"select * from cdr where src =".$userId; 都要改 ...

  8. spring boot 热启动

    spring boot热启动有两种方式 1. 以Maven插件的形式去加载,所以启动时使用通过Maven命令mvn spring-boot:run启动,而通过Application.run方式启动的会 ...

  9. 1.3 Quick Start中 Step 7: Use Kafka Connect to import/export data官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Step 7: Use Kafka Connect to import/export ...

  10. django遇到的那些古怪问题

    AssertionError: .accepted_renderer not set on Response 出错原因,没有在合法的方法内使用 response 响应,之前在dispatch内直接re ...