美文网首页
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的反向代理实例1

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