Nginx

Nginx 是一个高性能的Http和反向代理的Web服务器。

代理服务器

正向代理:可以隐藏客户端。 例:客户端通过访问VPN 去访问服务器。
反向代理:可以隐藏原始服务器。例:访问域名 ,域名可能对应多个服务器

Nginx提供负载均衡策略

内置策略:轮询、加权轮询、iphash(根据客户的ip获取hashCode,分配到指定服务器)

动静分离

把不需要后台处理的请求和静态资源(如:css,js,html,image等),可以做缓存操作,提高资源响应速率。

Linux 下载

wget http://nginx.org/download/nginx-1.18.0.tar.gz

tar -xvzf nginx-1.18.0.tar.gz -C /home/nginx

# 安装 (保存有问题,参考 https://blog.csdn.net/hybaym/article/details/50929958)
[root@iZwz9b8v7o84uupjlhulipZ nginx-1.18.0]# pwd
/home/nginx/nginx-1.18.0
[root@iZwz9b8v7o84uupjlhulipZ nginx-1.18.0]# ./configure   # 自动配置
#[root@iZwz9b8v7o84uupjlhulipZ nginx-1.18.0]# ./configure --prefix=/usr/local/nginx  # 手动指定配置位置

make install

# 安装成功后查找安装位置
[root@iZwz9b8v7o84uupjlhulipZ nginx-1.18.0]# whereis nginx
nginx: /usr/local/nginx

# 启动nginx
[root@iZwz9b8v7o84uupjlhulipZ sbin]# pwd
/usr/local/nginx/sbin
[root@iZwz9b8v7o84uupjlhulipZ sbin]# ./nginx              #重动
[root@iZwz9b8v7o84uupjlhulipZ sbin]# ./nginx -s reload    #重启(修改配置文件需要重启)
[root@iZwz9b8v7o84uupjlhulipZ sbin]# ./nginx -s quit      #安全退出
[root@iZwz9b8v7o84uupjlhulipZ sbin]# ./nginx -s stop      #停止

[root@iZwz9b8v7o84uupjlhulipZ sbin]# ps aux|grep nginx    #查看nginx进程

负载均衡配置

执行 ./configure报错时:
错误提示:
./configure: error: the HTTP rewrite module requires the PCRE library.
安装pcre-devel与openssl-devel解决问题

yum -y install pcre-devel openssl openssl-devel

nginx 的配置文件

#运行用户
#user  nobody;
#开启进程数 <=CPU数
worker_processes  1;
#错误日志保存位置
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#进程号保存文件
#pid        logs/nginx.pid;

#等待事件
events {
    #Linux下打开提高性能
    #use epoll;
    #每个进程最大连接数(最大连接=连接数x进程数)
    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;

    #设定请求缓冲
    client_header_buffer_size 1k;
    large_client_header_buffers 4 4k;

    #打开发送文件
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #客户端上传文件大小控制
    client_max_body_size 8m;

    #打开gzip压缩
    #gzip  on;
    #gzip_min_length      1000;  
    #gzip_types         text/plain text/css application/x-javascript;

    #设定负载均衡的服务器列表
    upstream mysvr {
        #weigth参数表示权值,权值越高被分配到的几率越大
        #根据ip计算将请求分配各那个后端tomcat,许多人误认为可以解决session问题,其实并不能。  
        #同一机器在多网情况下,路由切换,ip可能不同  
        server 127.0.0.1:8080 weight=10;
        #server 127.0.0.1:8090 weight=2;
    }

    #第一个虚拟主机
    server {
        #监听IP端口
        listen       80;
        #主机名
        server_name  localhostName;
        #root  

        #设置字符集
        #charset koi8-r;
        #本虚拟server的访问日志 相当于局部变量
        #access_log  logs/host.access.log  main;
        #日志文件输出格式
        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';

        location / {
           root   html;
           index  index.html index.htm;
           proxy_pass http://localhost:8080/warweb/;
        }

        #静态文件缓存时间设置
        location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ {         
            expires 30d;
        }

        #静态文件缓存时间设置
        location ~ .*.(js|css)?$ {         
            expires 1h;
        }

        #对本server"/"启用负载均衡
        #如果开启了这里的location,则79行的location必须屏蔽
        #对各种静态还是动态的数据进行过滤
        #此处如果请求是.jsp、.do结尾的文件都交给Tomcat服务器
        #其他的交给nginx处理
        # location ~ (.jsp)|(.do)$ {  
        #   proxy_pass http://mysvr;  
        #   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;  
        #   client_max_body_size 10m;  
        #   client_body_buffer_size 128k;  
        #   proxy_connect_timeout 90;  
        #   proxy_send_timeout 90;  
        #   proxy_read_timeout 90;  
        #   proxy_buffer_size 4k;  
        #   proxy_buffers 4 32k;  
        #   proxy_busy_buffers_size 64k;  
        #   proxy_temp_file_write_size 64k;  
        # }  
        #设定查看Nginx状态的地址
        # location /NginxStatus {
        #     stub_status on;
        #     access_log on;
        #     auth_basic “NginxStatus”;
        #     auth_basic_user_file conf/htpasswd;
        # }



        #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       localhost:50000;
    #     #主机名
    #     server_name  LIULJ2576;
    #     #WEB文件路径
    #     root         E:/Portal;
    #     #默认首页
    #     index        HomePage.html;        
    #     #location / {
    #     #    #这里相当于局部变量
    #     #    root   E:/Portal;
    #     #    index  HomePage.html;
    #     #}
    # }


    # HTTPS server HTTPS SSL加密服务器
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
原文地址:https://www.cnblogs.com/songl/p/14441369.html