您的位置:首页 > 运维架构 > 反向代理

Nginx反向代理基本配置

2016-03-15 12:23 796 查看
1.下载安装nginx

    yum install nginx

2.配置nginx

     vi /etc/nginx/nginx.conf

     配置详解

     user nginx;

     worker_processes 1;  //工作线程,即接收消息的线程数   一般情况下 可以使用auto  即cpu核心数

     error_log /var/log/nginx/error.log; //错误日志    建议修改路径到数据盘

     pid /run/nginx.pid; //线程pid

    

    events {

        use   epoll;  //网络模型   一般linux2.6以上版本都会支持epoll,此参数可省略

        worker_connections  1024;//单个worker process进程的最大并发链接数

    }

    http配置

    http {

        include       /etc/nginx/mime.types;

        default_type  application/octet-stream;

       

         #日志

         access_log    /var/log/nginx/access.log;//日志路径   此日志规模较大,建议将路径修改到一个数据盘

         log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';//日志格式化

         

          //文件系统配置   可省略

          sendfile on;   //linux 的系统调用 sendfile ,开启则直接发送,跳过内核缓冲区-程序缓冲区-SOCKET缓冲区。   下载应用可设为off来降低IO占用

          tcp_nopush     on; //在sendfile配置后可选,等待数据包大小合适

          tcp_nodelay        on;//与nopush互斥,立即发送,不必等数据包

          keepalive_timeout  65;//连接超时时间

   

  

          gzip  on;//开启gzip压缩

          

         //负载均衡设置    可省略
         upstream mysvr {

          }

          //默认服务器设置

          server {

                listen       80;//端口号

                server_name  www.xx.com;//_为接受全部请求

                #请求匹配   此处是反向代理

                location / {

                    proxy_next_upstream         http_502 http_504 error timeout invalid_header;

                    proxy_pass                  http://localhost:8080;
                    proxy_set_header            Host $host;

                    proxy_set_header            X-Real-IP $remote_addr;

                    proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;

                    client_max_body_size        100m;

                }

          }//server

           //HTTPS服务器设置

           server {

                 listen       443;

                 server_name  _;

                 ssl                  on;

                 ssl_certificate      /data/app/DouZhi/NewMJOpenssl/tencent/123.59.66.171.crt;

                 ssl_certificate_key  /data/app/DouZhi/NewMJOpenssl/tencent/123.59.66.171.key;

                 #ssl_client_certificate ca.crt

                 ssl_session_timeout  5m;

                  location / {

                         proxy_next_upstream         http_502 http_504 error timeout invalid_header;

                         proxy_pass                  http://localhost:51799;
                         proxy_set_header            Host $host;

                         proxy_set_header            X-Real-IP $remote_addr;

                         proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;

                        client_max_body_size        100m;

                 }

             }

     }//http

3.测试配置文件

    nginx -t /etc/nginx/nginx.conf

4.启动nginx

    nginx -s  reload
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: