Nginx教程

简单使用Nginx反向代理和负载均衡

本文主要是介绍简单使用Nginx反向代理和负载均衡,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

配置文件主要是三点:

events 、 http 、 server

配置反向代理和负载均衡策略

#配置tomcat的IP地址和访问端口||负载均衡:权重就是比例
     upstream guotong {
       		server 127.0.0.1:8088 weight=1;  
		server 127.0.0.1:8089 weight=1;  
		keepalive 64;		
     }

使用反向代理

        location / {
            root   html;
            index  index.html index.htm;
			proxy_pass   http://guotong/hello;
        }

配置基于Nginx的https:需要搞到SSL证书

 listen       443 ssl;
        server_name  localhost;

        ssl_certificate      G:\QMDownload\Nginx\OpenSSL\OpenSSL-Win64\bin\server.crt;
        ssl_certificate_key  G:\QMDownload\Nginx\OpenSSL\OpenSSL-Win64\bin\server.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

         location / { 
			# rewrite ^~/api/(.*)$ /$1 break;
            # add_header Access-Control-Allow-Origin *;
            # 允许客户端的请求方法
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE, PUT';
            # 允许客户端提交的的请求头
            add_header 'Access-Control-Allow-Headers' 'Origin, x-requested-with, Content-Type, Accept, Authorization';
            # 允许客户端提交Cookie
            add_header 'Access-Control-Allow-Credentials' 'true';
            # 允许客户端访问的响应头
            add_header 'Access-Control-Expose-Headers' 'Cache-Control, Content-Language, Content-Type, Expires, Last-Modified, Pragma';
 

            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-Port $server_port;
 
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
			#反向代理
			proxy_pass    http://guotong;
       }
#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;

	#配置tomcat的IP地址和访问端口||负载均衡:权重就是比例
     upstream guotong {
        server 127.0.0.1:8088 weight=1;  
		server 127.0.0.1:8089 weight=1;  
		keepalive 64;		
     }
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

		#反向代理到我们指定的负载均衡服务器
        location / {
            root   html;
            index  index.html index.htm;
			proxy_pass   http://guotong/hello;
        }

        #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      G:\QMDownload\Nginx\OpenSSL\OpenSSL-Win64\bin\server.crt;
        ssl_certificate_key  G:\QMDownload\Nginx\OpenSSL\OpenSSL-Win64\bin\server.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

         location / { 
			# rewrite ^~/api/(.*)$ /$1 break;
            # add_header Access-Control-Allow-Origin *;
            # 允许客户端的请求方法
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE, PUT';
            # 允许客户端提交的的请求头
            add_header 'Access-Control-Allow-Headers' 'Origin, x-requested-with, Content-Type, Accept, Authorization';
            # 允许客户端提交Cookie
            add_header 'Access-Control-Allow-Credentials' 'true';
            # 允许客户端访问的响应头
            add_header 'Access-Control-Expose-Headers' 'Cache-Control, Content-Language, Content-Type, Expires, Last-Modified, Pragma';
 

            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-Port $server_port;
 
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
			#反向代理
			proxy_pass    http://guotong;
       }
    }

}

这篇关于简单使用Nginx反向代理和负载均衡的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!