Welcome everyone

反向代理服务器 nginx

分布式 汪明鑫 675浏览 0评论

mac安装nginx

先安装brew

brew install nginx 
brew services start nginx

 

安装路径

MacBook-Pro:~ mac$ cd /usr/local/etc/nginx/
MacBook-Pro:nginx mac$ ls
fastcgi.conf		koi-win			scgi_params
fastcgi.conf.default	mime.types		scgi_params.default
fastcgi_params		mime.types.default	uwsgi_params
fastcgi_params.default	nginx.conf		uwsgi_params.default
koi-utf			nginx.conf.default	win-utf

配置文件 nginx.conf

 

 

访问一下:

http://localhost:8080/

 

linux安装nginx

tar -zxvf nginx-x.x.x

cd nginx-x.x.x

./configure --prefix=/root/nginx
make && make install

 

启动nginx

进入sbin目录

./nginx  -c  /root/nginx/conf/nginx.conf

nginx.conf 是nginx配置文件,启动nginx可以指定配置文件

 

nginx.conf

#user  nobody;
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 {
    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;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       81;   #监听的端口
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            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;
        #}
    }


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

}

 

这个监听的是81

 

nginx是由root用户启动,查看进程发现worker进程是nobody用户,nginx下的静态文件worker进程没有访问权限。

 

修改nginx配置文件conf/nginx.conf,将user配置修改与启动用户一致。

 

./sbin/nginx -s reload

再次访问成功

 

认识nginx

简单了解下nginx

nginx官网 :  http://nginx.org/

nginx wiki :  https://www.nginx.com/resources/wiki/

nginx 技术网站 :http://www.nginx.cn

 

 

Nginx是俄罗斯人编写的一款高性能的HTTP和反向代理服务器,在高连接并发的情况下,它能够支持高达50000个并发连接数的响应,但是内存、CPU等系统资源消耗却很低,运行很稳定。目前Nginx在国内很多大型企业都有应用,据最新统计,Nginx的市场占有率已经到33%左右了。而Apache的市场占有率虽然仍然是最高的,但是是呈下降趋势。而Nginx的势头很明显。选择Nginx的理由也很简单:第一,它可以支持5W高并发连接;第二,内存消耗少;第三,成本低,如果采用F5、NetScaler等硬件负载均衡设备的话,需要大几十万。而Nginx是开源的,可以免费使用并且能用于商业用途
  • 动静态资源分离——运用Nginx的反向代理功能分发请求:所有动态资源的请求交给Tomcat,而静态资源的请求(例如图片、视频、CSS、JavaScript文件等)则直接由Nginx返回到浏览器,这样能大大减轻Tomcat的压力。
  • 负载均衡,当业务压力增大时,可能一个Tomcat的实例不足以处理,那么这时可以启动多个Tomcat实例进行水平扩展,而Nginx的负载均衡功能可以把请求通过算法分发到各个不同的实例进行处理
  • 反向代理,现在大型网站分工详细,哪些服务器处理数据流,哪些处理静态文件,这些谁指挥,一般都是用nginx反向代理到内网服务器,这样就起到了负载均衡分流的作用。

 

 

 

反向代理应该是Nginx做的最多的一件事了,什么是反向代理呢

反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。简单来说就是真实的服务器不能直接被外部网络访问,所以需要一台代理服务器,而代理服务器能被外部网络访问的同时又跟真实服务器在同一个网络环境,当然也可能是同一台服务器,端口不同而已。

 

图解正向代理、反向代理(下图取自 芋道源码 一个很牛逼的公众号):

 

 

 

正向代理代理的是客户端

反向代理代理的是服务端

 

 

可以说nginx核心功能:反向代理 负载均衡

转载请注明:汪明鑫的个人博客 » 反向代理服务器 nginx

喜欢 (0)

说点什么

您将是第一位评论人!

提醒
avatar
wpDiscuz