nginx配置

文章url:

https://www.cnblogs.com/ysocean/p/9392912.html

#user  nobody;
worker_processes  2;#进程数

#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 {                         # 配置http服务器
    include       mime.types;  # 定义mime的文件类型
    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;     # 开启 sendfile 函数(zero copy 方式)输出文件
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  5;  # 连接超时时间,单位秒

    #gzip  on;
    upstream mynginx1 {        # 定义负载均衡设备的ip和状态
        #ip_hash;       #每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题    
        server 192.168.0.17:8085 weight=2;  # 值越高,负载的权重越高
        server 192.168.0.47:8081 weight=2;  # 值越高,负载的权重越高
        server 192.168.0.17:8084 weight=3;  # 默认权重值为一
        
        #server 192.168.0.17:8084;
        #server 192.168.0.17:8085;
        
        
        
        server 192.168.225.133:8083 down;       # 当前server 暂时不参与负载
        #server 192.168.225.133:8084 backup;     # 当其他非backup状态的server 不能正常工作时,才请求该server,简称热备
        #server mytest.com max_fails=3  fail_timeout=30s;
        #server index2.com;
    }
    server {                             # 设定虚拟主机配置
        listen       8088;                 # 监听的端口
        server_name  mynginx1;       # 监听的地址,多个域名用空格隔开
        
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {                         # 默认请求 ,后面 "/" 表示开启反向代理,也可以是正则表达式
            root   html;                     # 监听地址的默认网站根目录位置
            index  index.html;     # 欢迎页面
            proxy_pass http://mynginx1.com;  # 代理转发
            
            #deny 127.0.0.1;                      # 拒绝的ip
            allow 192.168.0.17;               # 允许的ip
            #allow all;
            #proxy_set_header Host $http_host;
        }

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

}

 注意:正则匹配时 ^~ /tomcat/   在地址proxy_pass http://myserver/;  一定得写/,否则一直提示404

upstream myserver {
    server 192.168.137.102:8080;
    server 192.168.137.102:8081;
    }
    server {
        listen       80;
        server_name  192.168.137.102;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location ^~ /tomcat/ {
             proxy_pass http://myserver/;
        }
        location /helloworld {
                return 603;
        }
        location / {
            root   html;
            proxy_pass http://myserver;
            index  index.html index.htm;
        }

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

 

基本配置

events {
  worker_connections  1024;  ## Default: 1024
} 
http {
     upstream testwebapi {  
        server 127.0.0.1:9001;  
        server 127.0.0.1:9002;  
     }
     server {
        listen      80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;
 
        client_max_body_size 20M;        
 
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
 
        location / {
              proxy_pass http://testwebapi;
              proxy_http_version 1.1;
              proxy_set_header Upgrade $http_upgrade;
              proxy_set_header Connection "";
              proxy_set_header Host $host:80;
              proxy_set_header X-NginX-Proxy true;
              proxy_set_header X-Real-IP $remote_addr;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
              proxy_cache_bypass $http_upgrade;
        }
  }
}

 

posted @ 2018-01-25 10:26  wjl910  阅读(123)  评论(0)    收藏  举报