Nginx

我们希望这个代理服务器可以帮助我们接收用户的请求,然后将用户的请求按照规则帮我们转发到不同的服务器节点之上。这个过程用户是无感知的,用户并不知道是哪个服务器返回的结果,我们还希望他可以按照服务器的性能提供不同的权重选择。保证最佳体验!所以我们使用了Nginx。

什么是Nginx?

Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务(邮件服务).

Nginx作用?

Http代理,反向代理:作为web服务器最常用的功能之一,尤其是反向代理。

正向代理概念 : 代理客户端的; 例如你要访问外网需要使用vnp去请求外网服务器,然后外网服务器再返回数据给到客户端.

反向代理:代理服务器; 多个客户端请求www.baidu.com 但是具体感知不到请求的哪一台服务器,客户端永远只需要访问这个域名就可以了.

负载均衡

1) 轮循: 依次轮流访问服务器

2) 权重: 根据服务器的性能分配,优先发送请求到性能较好的服务器上

3) iphash: iphash对客户端请求的ip进行hash操作,然后根据hash结果将同一个客户端ip的请求分发给同一台服务器进行处理,可以解决session不共享的问题。(建议使用redis来做session共享)

动静分离

动静分离,在我们的软件开发中,有些请求是需要后台处理的,有些请求是不需要经过后台处理的(如:CS5、 html、 jpg、 js等等文件),这些不需要经过后台处理的文件称为静态文件。让动态网站里的动态网页根据一定规则把不变的资源和经常变的资源区分开来,动静资源做好了拆分以后,我们就可以根据静态资源的特点将其做缓存操作。提高资源响应的速度。

Nginx的安装: 跨平台     官网: nginx   下载稳定版本

 下载完成后是一个压缩包,进行解压会得到一个目录

 

文件中默认配置监听80端口(可以省略不写)   访问本机地址: localhost 或 服务器地址(放开端口);

 Nginx常用命令

1 ./nginx 启动
2 ./nginx -s stop 停止
3 ./nginx -s quit 安全退出
4 ./nginx -s reload 重新加载配置文件
5 ps aux|grep nginx 查看nginx进程

nginx.conf配置文件


#user  nobody; # Nginx用户  
worker_processes  1;   # 工作进程:数目。根据硬件调整,通常等于CPU数量或者2倍于CPU
 
#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; 
    #每个工作进程的最大连接数量。根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行
}


http {
  # 这里是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;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

#  这里自定义代理名(fly) 给下面代理去使用 
    upstream fly {
      # 服务器资源  weight值如果一样就是轮循 (负载均衡配置)
      server 127.0.0.1:8080 weight=1;
      server 127.0.0.1:8081 weight=1;
    }

# 默认配置80端口 如果需要配置443端口 如下:
    server {
        listen       80;
        server_name  localhost;
        // 代理
        
    server {
        listen       443;
        server_name  localhost;
        // 代理

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://fly  #在这里引用配置  (反向代理)
        }

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

}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值