nginx介绍

nginx介绍Nginx 是什么 Nginx 是一款轻量级的 Web 服务器 反向代理服务器及电子邮件 IMAP POP3 代理服务器 其特点是占有内存少 并发能力强 事实上 nginx 的并发能力确实在同类型的网页服务器中表现较好 中国大陆使用 nginx 网站用户有 百度 京东 新浪 网易 腾讯 淘宝等

大家好,我是讯享网,很高兴认识大家。

6ea3f669ca1e4c9499de58e210294b7e.jpg
讯享网 Nginx是什么?

 

 

Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。

 

2 知识剖析

 

2.1 nginx服务器有什么作用?

 

1、反向代理

 

2、负载均衡

 

3、动静分离

 

2.2 什么叫反向代理?

 

反向代理:反向代理(ReverseProxy)是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,简单来说就是真实的服务器不能直接被外部网络访问,想要访问必须通过代理。

 

2.3 为什么要使用反向代理

 

1、防止主服务器被恶意攻击

 

2、为负载均衡和动静分离提供实现支持

 

2.4 什么是负载均衡?负载均衡的作用是什么?

 

负载均衡就是将任务分摊到多个操作单元上进行执行。对于Nginx而言,就是将收到的访问请求分发给不同的Web服务器,以提高访问性能以及可靠性。负载均衡可以有效防止一个服务器宕机而导致服务停止。

 

当一台服务器的单位时间内的访问量越大时,服务器压力就越大,大到超过自身承受能力时,服务器就会崩溃。为了避免服务器崩溃,让用户有更好的体验,我们通过负载均衡的方式来分担服务器压力。我们可以建立很多很多服务器,组成一个服务器集群,当用户访问网站时,先访问一个中间服务器,在让这个中间服务器在服务器集群中选择一个压力较小的服务器,然后将该访问请求引入该服务器。如此一来,用户的每次访问,都会保证服务器集群中的每个服务器压力趋于平衡,分担了服务器压力,避免了服务器崩溃的情况。

 

配置负载均衡:

 

http://nginx.org/en/docs/http/load_balancing.html

 

2.5 什么是动静分离?动静分离的作用?

 

动静分离:运用Nginx的反向代理功能分发请求:所有动态资源的请求交给应用服务器,而静态资源的请求(例如图片、视频、CSS、JavaScript文件等)则直接由Nginx返回到浏览器

 

动静分离的作用:主要是nginx处理静态页面的效率远高于tomcat的处理能力,使用c语言开发的nginx对静态资源每秒的吞吐量是使用Java语言开发的tomcat的6倍,也远高于其它应用服务器

 

3 常见问题

 

3.1 如何配置反向代理

 

通过配置实现

 

nginx的主配置文件名叫

 

nginx.config

它在nginx的安装目录下的config目录下面

 

复制代码

#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块主要有三个作用域, http server location

http { #可以嵌套多个server ,配置代理,缓存,日志,等绝大多数功能,和第三方模块配置

    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;

 

# 开启高效文件传输模式,这个指令,指定了nginx是否调用sendfile函数传输文件,对于普通的应用设为on ,如果用来进行下载等IO重负载应用可设为off

    sendfile on;

    #tcp_nopush on; #防止网络阻塞

 

    #keepalive_timeout 0;

    keepalive_timeout 65; # 长连接超时时常,单位秒

 

    #gzip on;

 

# 负载均衡

# upstream XXX.com{

# upstream负载均衡块, weight表示权重, 值越大,被分配到的几率就越大

# server 192.168.80.121:80 weight=3;

# server 192.168.80.121:80 weight=3;    

# server 192.168.80.121:80 weight=3;

#}

 

 

#server配置虚拟主机的相关参数

    server {  

        listen 80; # 监听端口

 

        server_name localhost; # 监听地址,可以采用域名加多个空格隔开,如果匹配不到我们在浏览器输入的域名.默认走 localhost, 然后location到nginx的欢迎页

    

        #charset koi8-r;

 

        # 本虚拟机的访问日志    

        #access_log logs/host.access.log main;

 

# location 配置请求的路由,和各种页面的处理情况

 

        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;

    # }

    #}

 

}

复制代码

我们要实现反向代理就是要

 

新增我们自己的server

复制代码

server {

        listen 80;

        server_name 浏览器可能访问的域名1;

 

        proxy_set_header X-Forwarded-Host $host;

        proxy_set_header X-Forwarded-Server $host;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

 

        location / {

            proxy_pass http://127.0.0.1:9001; # 转发路径

            proxy_connect_timeout 600;

            proxy_read_timeout 600;

        }

    }

    server {

        listen 80;

        server_name 浏览器可能访问的域名2;

 

        proxy_set_header X-Forwarded-Host $host;

        proxy_set_header X-Forwarded-Server $host;

        proxy

小讯
上一篇 2025-02-21 13:55
下一篇 2025-02-05 15:34

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/44977.html