美文网首页
nginx的反向代理实例1

nginx的反向代理实例1

作者: 攻城老狮 | 来源:发表于2020-08-30 22:45 被阅读0次

0. 实现效果

通过本地浏览器输入域名,可以实现通过nginx反向代理的操作,跳转到linux服务器的tomcat主页面的效果。

1. 准备工作

  • 配置java环境
  • 安装tomcat,并测试是否可以正常访问
  • 配置安装nginx

2. 具体配置

2.1 在windows系统的host文件中进行域名和ip地址的对应关系配置

  • 进入目录,打开hosts文件
C:\Windows\System32\drivers\etc\hosts
  • 在hosts文件中添加映射规则,此时可以通过该域名访问到指定ip的地址
# ip 域名
62.234.149.220 www.test.com

2.2 修改nginx的配置文件,修改访问nginx的ip地址以及转发的url路径

  • 进入conf目录
cd /usr/local/nginx/conf
  • 修改配置文件 nginx.conf
    • 修改server中的server_name,改为服务器的ip地址
    • 在server中的location中添加proxy_pass属性,用于定义转发的url路径
#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;

    #gzip  on;
#################################################################
    server {
        listen       80;
        server_name  62.234.149.220;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            proxy_pass http://62.234.149.220:8080;
            index  index.html index.htm;
        }
#################################################################
        #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;
    #    }
    #}

}

2.3 测试

  • 启动nginx
cd /usr/local/nginx/sbin
./nginx
  • 在本地浏览器测试,输入 www.test.com 便可成功访问到tomcat的主页面

相关文章

  • 玩转nginx

    本文内容包括: nginx配置实例之反向代理; nginx配置实例之动静分离; nginx配置实例之负载均衡; n...

  • 二、架构02-Nginx的反向代理、虚拟主机

    一、Nginx反向代理 1、环境:node01:192.168.32.132 Nginx (前端Nginx反向代理...

  • 第 4 章 nginx 配置实例-反向代理

    4.1 反向代理实例一 实现效果:使用 nginx 反向代理,访问 www.123.com 直接跳转到 127.0...

  • 01-nginx前端方向代理

    前端反向代理 1.下载nginx 2. 配置nginx.conf反向代理

  • Nginx的反向代理(实例1)

    Nginx的反向代理(实例1) 1.准备工作 把apache-tomcat-7.0.75.tar.gz 上传到/u...

  • nginx的反向代理实例1

    0. 实现效果 通过本地浏览器输入域名,可以实现通过nginx反向代理的操作,跳转到linux服务器的tomcat...

  • Nginx应用场景

    反向代理,负载均衡,动静分离 1.反向代理 修改nginx配置,并重新加载 重新加载nginx配置./nginx ...

  • nginx配置说明

    Nginx常用功能1、Http代理,反向代理:作为web服务器最常用的功能之一,尤其是反向代理。Nginx在做反向...

  • nginx做代理上网

    nginx不仅可以来做反向代理,也可以用来做正向代理(透明代理,代理上网),nginx反向代理看这里nginx反向...

  • 第十九周作业

    1、搭建Tomcat集群,并通过nginx反向代理访问 反向代理示意图 测试环境: Nginx: 172.16.1...

网友评论

      本文标题:nginx的反向代理实例1

      本文链接:https://www.haomeiwen.com/subject/mfdysktx.html