Nginx专题三 : Nginx反向代理配置

需求一:

1.浏览器请求Nginx(http://111.229.248.243:9003);
2.Nginx将请求转发至目标服务器(tomcat页面);


image.png

需求二:

需求一基础上新增两个目标服务:
1.当访问:http://111.229.248.243:9003/abc 的时候,实际访问目标是:172.0.0.1:8080
2.当访问:http://111.229.248.243:9003/def 的时候,实际访问目标是:172.0.0.1:8081

image.png

需求一配置实现:

1.部署tomcat,保持默认监听8080端口;
2.修改nginx配置;


image.png

3.重新加载配置

./nginx -s reload

4.测试访问:http://111.229.248.243:9003,返回tomcat页面


image.png

需求二实现:

1.部署tomcat,保持默认监听8081端口;
2.修改nginx配置;

image.png

多location的使用:
对应location语法:

location [=||*|^~] /uri/ { … }

在nginx配置⽂件中,location主要有这⼏种形式:
1)正则匹配 location ~ /lagou { }
2)不区分⼤⼩写的正则匹配 location ~* /lagou { }
3)匹配路径的前缀 location ^~ /lagou { }
4)精确匹配 location = /lagou { }
5)普通路径前缀匹配 location /lagou { }
优先级:4 > 3 > 2 > 1 > 5
3.重新加载配置

./nginx -s reload

nginx配置文件如下:

#==========================================全局块-start===============================
#运行用户
#user  nobody;
#worker进程数量,通常设置为和cpu数量相等
worker_processes  1;

#全局错误日志及pid文件位置
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

#=======================================全局块-start===========================================

#=======================================events块-end===========================================
events {
    #单个worker进程的最大并发连接数
    worker_connections  1024;
}
#=======================================events块-end===========================================

#=======================================http块-start(nginx服务器中配置最频繁的部分,端口监听,配置转发)===========================================
http {
    # 引入mime类型定义文件
    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;
    #开启zip压缩
    #gzip  on;

    server {
        #监听的端口
        listen       9003;
        #定义使用localhost访问
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        #默认请求
        location / {
            #root   html; #默认的网站根目录位置
            #index  index.html index.htm; #索引页,欢迎页
            proxy_pass http://127.0.0.1:8080
        }
        
        location /abc {
            #root   html; #默认的网站根目录位置
            #index  index.html index.htm; #索引页,欢迎页
            proxy_pass http://127.0.0.1:8080
        }
        
        location /def {
            #root   html; #默认的网站根目录位置
            #index  index.html index.htm; #索引页,欢迎页
            proxy_pass http://127.0.0.1:8081
        }

        
        #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;
    #    }
    #}

}
#=======================================http块-end===========================================

引用:https://blog.csdn.net/weixin_42767604/article/details/105298277

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容