nginx 代理服务器常规配置

# 配置启动用户,用户权限不够会出现访问 403 的情况
user root;

# 启动多少个工作进程
worker_processes  1;

# 错误日志文件进程文件的保存地址
error_log  logs/error.log;
pid        logs/nginx.pid;

# 事件配置项 worker_connections 一个工作进程能处理的最大连接数
events {
    worker_connections  1024;
}

http {

    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
    
    # 自定义的日志模式 《名称》《日志要显示信息》
    # $remote_addr 来访者 IP
    # $http_x_forwarded_for 使用代理时真正的用户 IP
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
                      
    # 上游服务器(服务器组,用于负载均衡)<名称不能使用下划线拼接。切记切记!>
    # upstream imageserver {
    #    服务    IP                 权重      最大失败数   失败时的请求间隔
    #    server 192.168.200.100:80 weight=1 max_faild=2 fail_timeout=30s;
    # }

    # 一个 Server 就是一个服务
    server {
        
        # 监听的端口
        listen       80;
        
        # 服务的名称
        server_name  localhost;

        # 编码配置
        #charset koi8-r;

        # 开启日志自定义 《保存地址》 《使用哪个自定义模式》
        access_log  logs/access.log  main;

        # 配置项目所在路径和默认访问页面
        location / {
            root   html;
            index  index.html index.htm;
        }
        
        # 反向代理, 将所有的图片请求转发到特定服务器 
        # imageserver 为自定义的上有服务器,如果不做负载均衡,这里可以直接写IP地址
        # 为了让被代理的服务器获取客户端的真实IP,应当遵循代理服务器协议
        # 需要在请求头中添加一个参数 X-Forwarded-For 参数,值为用户IP
        # location ~ .(jpeg|jpg|png|gif)$ {
        #    proxy_pass http://imageserver;
        #   proxy_set_header X-Forwarded-For $remote_addr;
        # }

        # 页面访问出错为 404 时返回
        error_page 404              /404.html;

        # 页面返回出错我 50X 是返回
        error_page 500 502 503 504  /50x.html;

        # 关于联合整合 PHP 的相关配置
        # 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;
        # }

        # 拒绝访问的文件
        location ~ /.ht {
            deny  all;
        }
    }

    # HTTPS 服务的相关配置
    # 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;
    #    }
    # }

}

负载均衡策略请参考 => https://www.cnblogs.com/andashu/p/6377323.html

原文地址:https://www.cnblogs.com/lovling/p/9295030.html