nginx配置文件

发布时间:2023年12月30日


前言

nginx是一款高性能的代理服务器,本篇文章介绍nginx.conf配置文件,安装请看这里


一、nginx.conf

#user  nobody;
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       8099;
        server_name  localhost;
        location / {
            root   resource;
            index  index.html index.htm;
            try_files $uri /index.html;
        }
        location /gdp {
            alias resource/gdp;
            proxy_set_header Host $host:$server_port;
        }
        location /sys {
            alias /home/sys/app/system/web/sys;
            try_files $uri $uri/ /index.html;
        }
        location /api/ {
            proxy_pass  http://127.0.0.1:2005/;
            proxy_set_header x-real-ip $remote_addr;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

二、负载均衡

#user  nobody;
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    #新增-start
    upstream sysservers {
        server 127.0.0.1:2005 weight=1;
        server 127.0.0.2:2005 weight=1;
        server 127.0.0.3:2005 weight=1;
    }
    #新增-end
    server {
        listen       8099;
        server_name  localhost;
        location / {
            root   resource;
            index  index.html index.htm;
            try_files $uri /index.html;
        }
        location /gdp {
            alias resource/gdp;
            proxy_set_header Host $host:$server_port;
        }
        location /sys {
            alias /home/sys/app/system/web/sys;
            try_files $uri $uri/ /index.html;
        }
        #修改proxy_pass
        location /api/ {
            proxy_pass  http://sysservers/;
            proxy_set_header x-real-ip $remote_addr;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

三、转发post请求

nginx默认将接收到的请求一get的形式转发出去,但是请求中不免有post或者其它类型的请求

location /gdp/sd {
    proxy_pass  http://sysservers/gdp/sd;
    proxy_set_header x-real-ip $remote_addr;
    proxy_connect_timeout 600s;
    proxy_send_timeout 600s;
    #主要是这个参数
    proxy_redirect off;
}

四、转发socket请求

stream 和http标签同级

stream {
    upstream socketservers {  
        server 127.0.0.1:8080;  
        server 127.0.0.2:8080;
        server 127.0.0.3:8080;
    }
    server {
        listen 8080;
        proxy_pass socketservers;
    }
}

总结

回到顶部

文章来源:https://blog.csdn.net/qq_44824164/article/details/135303412
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。