Nginx入门级安装和基础使用

Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。由俄罗斯的程序设计师Igor Sysoev所开发,官方测试nginx能够支支撑5万并发链接,并且cpu、内存等资源消耗却非常低,运行非常稳定。

应用场景

  1. http服务器。Nginx是一个http服务可以独立提供http服务。可以做网页静态服务器
  2. 虚拟主机。可以实现在一台服务器虚拟出多个网站。例如个人网站使用的虚拟主机。
  3. 反向代理,负载均衡。当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用nginx做反向代理。并且多台服务器可以平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。

 

nginx安装

下载nginx:

官方网站:

http://nginx.org/

要求的安装环境

  1. 需要安装gcc的环境。yum install gcc-c++
  2. 第三方的开发包。   
  • PCRE

       PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。

        yum install -y pcre pcre-devel

注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。

  • zlib

       zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。

       yum install -y zlib zlib-devel

  • openssl

       OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。

       nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。

       yum install -y openssl openssl-devel

安装步骤

第一步:把nginx的源码包上传到linux系统

第二步:解压缩

[root@localhost ~]# tar zxf nginx-1.8.0.tar.gz

第三步:使用configure命令创建一makeFile文件。

./configure && make && make install

 

启动nginx

进入sbin目录

[root@localhost sbin]# ./nginx

关闭nginx:

[root@localhost sbin]# ./nginx -s stop

推荐使用:

[root@localhost sbin]# ./nginx -s quit

重启nginx:

  1. 先关闭后启动。
  2. 刷新配置文件:

[root@localhost sbin]# ./nginx -s reload

访问nginx

默认是80端口。

注意:是否关闭防火墙。

配置虚拟主机

就是在一台服务器启动多个网站。如何区分不同的网站:

  1. 域名不同
  2. 端口不同  
  1.  

通过端口区分不同虚拟机

Nginx的配置文件:

/usr/local/nginx/conf/nginx.conf

重新加载配置文件

[root@localhost nginx]# sbin/nginx -s reload

Nginx实现反向代理

第一步:安装两个tomcat,分别运行在8080和8081端口。

第二步:启动两个tomcat。

第三步:反向代理服务器的配置

负载均衡

如果一个服务由多条服务器提供,需要把负载分配到不同的服务器处理,需要负载均衡。

upstream tomcat00 {
            server 192.168.25.128:8080;
            server 192.168.25.128:8081;
        }
    server {
        listen       80;
        server_name  www.test.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

可以根据服务器的实际情况调整服务器权重。权重越高分配的请求越多,权重越低,请求越少。默认是都是1

upstream tomcat00 {
            server 192.168.25.128:8080;
            server 192.168.25.128:8081 weight=2;
        }

猜你喜欢

转载自blog.csdn.net/tonglei111/article/details/109051178