Nginx的安装和部署

Nginx简介

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

优势

Nginx 可以在大多数 Unix Linux OS 上编译运行,并有 Windows 移植版。 Nginx 的1.20.0稳定版已经于2021年4月20日发布,一般情况下,对于新建站点,建议使用最新稳定版作为生产版本,已有站点的升级急迫性不高。Nginx 的源代码使用 2-clause BSD-like license
Nginx 是一个很强大的高性能Web反向代理服务,它具有很多非常优越的特性:

  • 在连接高并发的情况下,NginxApache服务不错的替代品:Nginx在美国是做虚拟主机生意的老板们经常选择的软件平台之一。能够支持高达 50,000 个并发连接数的响应。
  • Nginx作为负载均衡服务:Nginx 既可以在内部直接支持 Rails 和 PHP 程序对外进行服务,也可以支持作为 HTTP代理服务对外进行服务。Nginx采用C进行编写,不论是系统资源开销还是CPU使用效率都比 Perlbal 要好很多。
  • 处理静态文件索引文件以及自动索引,打开文件描述符缓冲
  • 无缓存的反向代理加速,简单的负载均衡容错
  • FastCGI,简单的负载均衡容错
  • 模块化的结构。包括 gzipping, byte ranges, chunked responses,以及 SSI-filter 等 filter。如果由 FastCG或其它代理服务器处理单页中存在的多个 SSI,则这项处理可以并行运行,而不需要相互等待。
  • 支持 SSLTLSSNI
  • Nginx代码完全用C语言从头写成,已经移植到许多体系结构和操作系统,包括:Linux、FreeBSD、Solaris、Mac OS X、AIX以及Microsoft Windows。Nginx有自己的函数库,并且除了zlib、PCRE和OpenSSL之外,标准模块只使用系统C库函数。而且,如果不需要或者考虑到潜在的授权冲突,可以不使用这些第三方库。
  • 代理服务器。作为邮件代理服务:Nginx 同时也是一个非常优秀的邮件代理服务(最早开发这个产品的目的之一也是作为邮件代理服务器),Last.fm 描述了成功并且美妙的使用经验。
  • Nginx 是一个安装非常的简单、配置文件非常简洁(还能够支持perl语法)、Bug非常少的服务。Nginx 启动特别容易,并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动。你还能够不间断服务的情况下进行软件版本的升级。

使用

下载

官网下载最新稳定版

image-20210511134526594

从源代码构建

如果需要某些特殊功能,但软件包和端口不提供这些功能,则也可以从源文件编译nginx。虽然更灵活,但是这种方法对于初学者来说可能很复杂。有关更多信息,请参见从源代码构建nginx。

安装

下载完成后解压到不含中文(切记!)的目录

image-20210511134646549

启动

两种方法:

  1. 直接双击该目录下的"nginx.exe",即可启动nginx服务器;

  2. 命令行进入该文件夹,执行start nginx命令,也会启动nginx服务器。
    使用 Win+R打开运行,输入cmd,然后按Enter
    首先进入nigix所在的目录,如下图所示:

D:
cd Environment/nginx-1.20.0 #这是我解压的目录,替换成自己的即可

image-20210511135323876

启动:start nginx.exe

停止:nginx.exe -s stop

重新加载:nginx.exe -s reload

使用http://localhost:端口查看

那么怎么知道自己的nginx是否启动成功:打开你的管理器,如在进程中看到两个nginx说明启动成功。

image-20210511141124049

打开浏览器输入http://localhost,显示以下界面即表示启动成功

image-20210511141246443

反代理Tomcat

反向代理动态页面

首先用Tomcat启动一个JavaWeb项目,Tomcat端口号为8080,响应内容如下

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    req.setCharacterEncoding("utf-8");
    resp.setCharacterEncoding("utf-8");
    PrintWriter out = resp.getWriter();

    Cookie[] cookies = req.getCookies();
    if (cookies == null) {
        out.write("First");
    } else {
        out.write("上次时间是:");
        for (int i = 0; i < cookies.length; i++) {
            if (cookies[i].getName().equals("lastTime")) {
                long l = Long.parseLong(cookies[i].getValue());
                String s = new Date(l).toString();
                out.write(s);
            }

        }
    }
    resp.addCookie(new Cookie("lastTime", System.currentTimeMillis()+""));

}

每次刷新都会动态从Cooki中获取上次时间,启动后浏览器访问http://localhost:8080/c1即可查看效果

img

然后编辑Nginx配置文件,新增一个包

upstream tomcatserver {
    server localhost:8080;
}

然后编辑server闭包,修改如下内容

Listen 80;
    server_name localhost;
location / {
    proxy_pass http://tomcatserver;
}

之后启动Nginx,访问localhost/c1即可预览效果

img

则表示正确代理了动态页面

Nginx的配置

配置文件位于安装目录下的/conf/nginx.conf文件

server {
    listen       80; #监听的端口
    server_name  localhost; #监听的域名
    #charset koi8-r;
    #access_log  logs/host.access.log  main;
    location / {
		#root html;
		#index index.html index.html;
        proxy_pass http://127.0.0.1:8081; #转发请求的地址
		proxy_connect_timeout 600;
		proxy_read_timeout 600;
}

Windows下的常用命令

启动服务:start nginx
退出服务:nginx -s quit
强制关闭服务:nginx -s stop
重载服务:nginx -s reload  (重载服务配置文件,类似于重启,服务不会中止)
验证配置文件:nginx -t
使用配置文件:nginx -c "配置文件路径"
使用帮助:nginx -h

Linux下的安装

一、安装编译工具及库文件

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

二、首先要安装 PCRE

PCRE 作用是让 Nginx 支持 Rewrite 功能。

1、下载 PCRE 安装包,下载地址: http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz

cd /usr/local/src/
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz

2、解压安装包:

tar zxvf pcre-8.35.tar.gz

3、进入安装包目录

cd pcre-8.35

4、编译安装

./configure
make && make install

5、查看pcre版本

pcre-config --version

安装 Nginx

1、下载 Nginx,下载地址:https://nginx.org/en/download.html

cd /usr/local/src/
wget http://nginx.org/download/nginx-1.20.0.tar.gz

2、解压安装包

tar zxvf nginx-1.20.0.tar.gz

3、进入安装包目录

cd nginx-1.20.0

4、编译安装

./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35
make
make install

到此,nginx安装完成。


Nginx 配置

nginx.conf说明

#user  nobody;
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; # nginx进程pid存放路径

events {
    worker_connections  1024; # 工作进程的最大连接数量
}


http {
    include       mime.types; #指定mime类型,由mime.type来定义
    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; #用log_format指令设置日志格式后,需要用access_log来指定日志文件存放路径
    				
    sendfile        on; #指定nginx是否调用sendfile函数来输出文件,对于普通应用,必须设置on。如果用来进行下载等应用磁盘io重负载应用,可设着off,以平衡磁盘与网络io处理速度,降低系统uptime。
    #tcp_nopush     on; #此选项允许或禁止使用socket的TCP_CORK的选项,此选项仅在sendfile的时候使用
 
    #keepalive_timeout  0;  #keepalive超时时间
    keepalive_timeout  65;
     
    #gzip  on; #开启gzip压缩服务
     
    #虚拟主机
    server {
        listen       80;  #配置监听端口号
        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$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
        #    root           html; #根目录
        #    fastcgi_pass   127.0.0.1:9000; #请求转向定义的服务器列表
        #    fastcgi_index  index.php; # 如果请求的Fastcgi_index URI是以 / 结束的, 该指令设置的文件会被附加到URI的后面并保存在变量$fastcig_script_name中
        #    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; # ssl_prefer_server_ciphers  on; #

 

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

启动nginx服务

切换目录到/usr/local/nginx/sbin下面

image-20210511233708251

启动nginx命令:

./nginx

查看nginx服务是否启动成功

ps -ef | grep nginx

image-20210511233800456

访问站点

从浏览器访问我们配置的站点ip:

image-20210511141246443

Nginx 其他命令

以下包含了 Nginx 常用的几个命令:

启动服务:./nginx
退出服务:nginx -s quit
强制关闭服务:nginx -s stop
重载服务:nginx -s reload  (重载服务配置文件,类似于重启,但服务不会中止)
验证配置文件:nginx -t
使用配置文件:nginx -c "配置文件路径"
使用帮助:nginx -h

posted @ 2021-07-17 11:08  超凡丶  阅读(9753)  评论(0编辑  收藏  举报