简单使用Nginx反向代理和负载均衡
2021/11/16 7:13:21
本文主要是介绍简单使用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反向代理和负载均衡的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-29Nginx发布学习:从入门到实践的简单教程
- 2024-10-28Nginx发布:新手入门教程
- 2024-10-21nginx 怎么设置文件上传最大20M限制-icode9专业技术文章分享
- 2024-10-17关闭 nginx的命令是什么?-icode9专业技术文章分享
- 2024-09-17Nginx实用篇:实现负载均衡、限流与动静分离
- 2024-08-21宝塔nginx新增8022端口方法步骤-icode9专业技术文章分享
- 2024-08-21nginx配置,让ws升级为wss访问的方法步骤-icode9专业技术文章分享
- 2024-08-15nginx ws代理配置方法步骤-icode9专业技术文章分享
- 2024-08-14nginx 让访问带有/relid的地址返回404 ,例子 /relid-x-0.36-y-131.html-icode9专业技术文章分享
- 2024-08-14nginx 判断地址有/statics/的路径,指向到/home/html/statics/目录-icode9专业技术文章分享