nginx 负载均衡、逆向代理、前后端分离。

    技术2024-10-17  55

    nginx 配置文件、常用命令

    一、命令

    nginx.exe nginx -s stop nginx -s reload nginx -t //测试Nginx的配置是否正确

    二、配置文件包含三部分内容

    (1)全局块:配置服务器整体运行的配置指令

    比如 worker_processes 1;处理并发数的配置

    (2)events 块:影响 Nginx 服务器与用户的网络连接

    比如 worker_connections 1024; 支持的最大连接数为 1024

    (3)http 块还包含两部分: 1、 http 全局块

    2、server 块

    #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; client_max_body_size 1024m; #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; upstream myserver{ server 127.0.0.1:8080; server 127.0.0.1:8081; } server { # 监听端口号 listen 9001; server_name localhost; # location /www/{ # root F:/nginxTest/; # autoindex on; # } # location /html/ { # root F:/nginxTest/; # } # location ~ / { # proxy_pass http://myserver; # } location ~ /dev/ { proxy_pass http://localhost:8081; }

    基本概念

    1、正向代理

    (1)需要在客户端配置代理服务器进行指定网站访问

    2、反向代理

    暴露的是代理服务器地址,隐藏了真实服务器 IP 地址。

    3、负载均衡 4、动静分离

    nginx 实现反向代理

    1、nginx 配置

    ser ver { # 监听端口号 listen 9001; server_name localhost; # location /www/{ # root F:/nginxTest/; # autoindex on; # } # location /html/ { # root F:/nginxTest/; # } # location ~ / { # proxy_pass http://myserver; # } location ~ /dev/ { proxy_pass http://localhost:8081; } location ~ /test/ { proxy_pass http://localhost:8080; }

    2、tomcat 配置

    一、8080

    二、8081

    1、配置文件 (修改)

    <Server port="8006" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> <!-- Security listener. Documentation at /docs/config/listeners.html <Listener className="org.apache.catalina.security.SecurityListener" /> <Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

    2、资源配置

    3、测试

    一、tomcat 测试 二、nginx 测试

    nginx 实现负载均衡

    1、nginx 配置

    upstream myserver{ server 127.0.0.1:8080; server 127.0.0.1:8081; } server { # 监听端口号 listen 9001; server_name localhost; location ~ / { proxy_pass http://myserver; }

    2、tomcat 配置

    3、测试 一、二、

    nginx 实现动静分离

    1、nginx 配置

    # 监听端口号 listen 9001; server_name localhost; location /www/{ root F:/nginxTest/; autoindex on; } location /html/ { root F:/nginxTest/; }

    2、静态资源配置

    一、 二、 3、测试

    一、访问 www

    二、访问 html/a.html

    nginx 配置多配置文件

    目录如下

    config / configTest.conf

    server { listen 80; server_name a.com; location / { root D:/nginx/web1; index index.html index.htm; } } server { listen 81; server_name d.com; location /web2/ { alias D:/nginx/; autoindex on; } } server { listen 82; server_name c.com; location / { root D:/nginx/web3; index index.html index.htm; } }

    nginx.conf

    #user nobody; worker_processes 1; events { worker_connections 1024; } http { include config/*.conf; include mime.types; default_type application/octet-stream; client_max_body_size 1024m; sendfile on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { # 监听端口号 listen 9001; server_name localhost; location /www/{ alias E:/nginxTest/; autoindex on; } location /html/ { alias E:/nginxTest/; } location /api/ { proxy_pass http://127.0.0.1:8888/api/private/v1/;#转发请求的地址 proxy_connect_timeout 6000;#链接超时设置 proxy_read_timeout 6000;#访问接口超时设置 } } }
    Processed: 0.012, SQL: 9