windows版 nginx配置反向代理实例教程 跳转tomcat和php网站

抄自

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服务器做反向代理服务器的全部内容,希望对你有所帮助。

转载于:https://www.cnblogs.com/zonglonglong/p/10689821.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值