Nginx.conf反向代理配置详解

本文介绍Nginx的基本配置及如何实现简单的负载均衡。通过配置upstream模块,可以将请求分发到多个后端服务器,提高网站的可用性和响应速度。文章还涉及错误日志记录、连接数设置等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



#user  nobody;
worker_processes  1;#设置nginx服务的worker子进程,比如设为2:


error_log  logs/error.log; #去掉前面的#,记录nginx错误日志,方便检查bug:
error_log  logs/error.log  notice;
error_log  logs/error.log  info;


#pid        logs/nginx.pid;  #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;
#这里很关键,很多小伙伴问我 “负载均衡乍配置,为啥我配置的不能访问呢“,这里的upstream就是配置负载均衡的,当然得两台以上才叫负载,我这里的ip69和68都是
#用的apache,   也许你们的是tomcat, 没关系,按这样配置一样可以,
 upstream proxy_test {
   server 127.0.0.1:8080 weight=1;     #如果你要测试,把这里换成你自己要代理后端的ip
   server 127.0.0.1:8081 weight=1;
   ip_hash;                                              #当负载两台以上用ip_hash解决session的问题,一台就别hash了。
 }
#这是server段的配置
    server {
        listen       80;
        server_name  localhost;#要访问的域名,我这里用的测试域名,如果有多个,用逗号分开


        charset utf8;


        #access_log  logs/host.access.log  main;


        location / {
           # root   html;
           # index  index.html index.htm;
    proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://proxy_test;  #这里proxy_test是上面的负载的名称,映射到代理服务器,可以是ip加端口,   或url 
        }
access_log logs/manageplatform.log;


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


}
### Nginx 反向代理配置详解 要实现 Nginx反向代理功能,可以通过编辑 `nginx.conf` 文件来完成。以下是详细的配置说明: #### 基本结构 Nginx配置文件由多个模块组成,主要包括全局块、events 块、http 块以及 server 块等。对于反向代理而言,主要涉及的是 **http** 和 **server** 块。 --- #### 配置示例 以下是一个典型的 Nginx 反向代理配置示例[^1]: ```nginx user nginx; worker_processes auto; error_log /var/log/nginx/error.log notice; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream backend { server xxx.xxx.xxx.xxx:8081; # 后端服务器地址和端口 } server { listen 80; # 监听的端口号 server_name localhost; # 主机名或域名 location /tomcat { # 定义路径前缀 proxy_pass http://backend/; # 将请求转发至后端服务器 } } } ``` 在此配置中: - 使用了 `upstream` 来定义一组后端服务器(可扩展为负载均衡场景)。如果仅有一个目标服务器,则可以直接在 `proxy_pass` 中指定其 URL。 - `location` 块中的 `/tomcat` 表明只有以该路径开头的请求会被代理到后端服务器。 --- #### WebSocket 支持 如果需要支持 WebSocket 协议,在 HTTP 块中需额外添加一些配置项[^3]: ```nginx http { ... map $http_upgrade $connection_upgrade { default upgrade; '' close; } server { listen 9016; server_name localhost; location /ws { proxy_pass http://xxx.xxx.xxx.xxx:9016/ws; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; } } } ``` 此部分的关键在于启用 `Upgrade` 头部字段的支持,并确保协议升级机制正常运作。 --- #### 验证配置文件有效性 完成配置修改后,建议使用以下命令验证语法是否正确[^2]: ```bash /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf ``` 或者简化版本: ```bash nginx -t ``` 若无误,重新加载配置即可生效: ```bash nginx -s reload ``` --- #### 生产环境优化 为了提高性能和稳定性,可以根据实际需求调整以下几个参数[^4]: - 设置工作线程数量为 CPU 核心数: ```nginx worker_processes auto; ``` - 调整单个工作进程的最大连接数: ```nginx events { worker_connections 1024; } ``` - 添加 MIME 类型支持并设置默认类型: ```nginx include mime.types; default_type application/json; ``` - 开启缓存加速静态资源访问: ```nginx sendfile on; tcp_nopush on; tcp_nodelay on; ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值