初识Nginx一篇足以,Nginx入门教程,实战演示。


前言

       随着网站用户访问量的不断增加,网站的响应时间也在慢慢的增加,甚至有的时候会出现网站奔溃的情况。这说明一台服务器已经满足不了我们网站的需求了,于是我们就增加多台服务器,但是如何把这些服务器用统一的域名来管理呢?我的答案是Nginx,本文就向大家简单介绍Nginx,并且教大家如何简单入门。

一、Nginx是什么?

Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。因它的稳定性、丰富的功能集、简单的配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。

       其主要的特点还有:占有内存少,并发能力强,并且Nginx 启动特别容易,并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动。你还能够不间断服务的情况下进行软件版本的升级。
       并且你要知道一个传统Tomcat能够承受的并发访问量大概是800个左右,而根据Nginx官方数据测试表明Nginx能够支持高达 50,000 个并发连接数的响应。以此可见Nginx的强大。

二、使用步骤

1.下载Nginx

       打开Nginx官网:http://nginx.org/en/download.html,这里我们选择稳定版本下载即可。
在这里插入图片描述
       下载完成之后我们会得到一个压缩包,解压就可以使用了。
在这里插入图片描述

2.配置Nginx监听

       下载完Nginx我们就可以使用了,我们可以先看一下Nginx的配置文件(在conf目录下有个nginx.conf文件)。
在这里插入图片描述

在这里插入图片描述
       这个配置文件我们可以修改Nginx代理的主机和端口,这里Nginx默认代理的是localhost:80。

3.启动Nginx

       现在我们开启Nginx,体验一下,启动方式有以下几种:

  • 直接双击nginx.exe文件,这种方式会出现一个一闪而过的窗口
  • 打开cmd命令控制窗口,切换到nginx目录下,输入nginx.exe回车即可(推荐方式)

在这里插入图片描述
       然后我们就可以去访问localhost:80了,也直接可访问localhost,因为浏览器访问端口默认是80。
在这里插入图片描述
       看到以上页面说明我们Nginx开启成功了!

4.关闭Nginx

       使用cmd命令窗口启动nginx,关闭cmd窗口是无法关闭nginx进程的,我们需要使用以下命令来结束nginx进程:

  • 重新打开一个cmd窗口,输入nginx命令nginx -s stop(快速停止nginx) 或 nginx -s quit(完整有序的停止nginx)
  • 使用taskkill taskkill /f /t /im nginx.exe
1. taskkill是用来终止进程的。
2. /f是强制终止。
3. /t终止指定的进程和任何由此启动的子进程。
4. /im示指定的进程名称。

关闭时可能出现的问题:

报错信息:nginx: [error] CreateFile() "D:nginx-1.20.2nginx-1.20.2/logs/nginx.pid" failed (2: The system cannot find the file specified)

在这里插入图片描述
       发现两种方式都无法正常关闭Nginx,并且报错信息中logs文件夹下也没有nginx.pid文件。没办法,只好进入任务管理器,强制杀死进程。
在这里插入图片描述
       然后我再去尝试用指令开启Nginx,再去用指令nginx -s -stop去关闭Nginx,竟然又可以正常关闭了,很奇怪,为什么第一次关闭就不行呢?-----------薛定谔的Nginx

三、体验Nginx

       通过上面的学习,我们对nginx有了基本的认识,接下来我们实战进行体验。我们可以给一个完整的项目配置Nginx。

问题:我们假设现在在我们电脑上分别开放了两个端口(8080和8081)用于提供服务,现在我们需要使用Nginx将这两个端口用80代替,即用户访问80端口即可。怎么做呢?

在这里插入图片描述

1、修改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       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$ {
        #    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;
    #    }
    #}

}

       在这里为了方便理解,我简化了配置文件的主要内容(模板解读):

上面这一部分是nginx的全局配置,目前我们可以不用管。


events {
    worker_connections  1024;
}	//这一部分是配置Nginx最大连接数量等等。

http{
	http的一些配置

	upstream ${name}{
		//负载均衡的主机端口,就是需要代理的主机。
	}

	server{
		listen	xx;
		server_name		xxx;
		// 代理

		location  /xxx{
			//访问该服务器的xx端口的/xxx路由就会进入到这里,访问这个location下的资源信息
		}

		location  /admin{
			//访问该服务器的xx端口的/admin路由就会进入到这里,访问这个location下的资源信息
		}
	}
}

2、配置Nginx信息

       现在我们就开始配置上面那个案例了(用#####囊括的内容是需要我们添加的内容)。


#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;
####### 配置负载均衡
	upstream testName{
		#服务器资源
		server 127.0.0.1:8080	weight=1;
		server 127.0.0.1:8081	weight=1;
	}
#######
    server {
        listen       80;
        server_name  localhost;
		
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
###### 代理
            proxy_pass http://testName;
######
        }

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

}

       以上就是我们需要配置的内容了,现在我们保存该文件,然后重新加载或者重新启动Nginx访问本地localhost:80端口即可进入到了我们项目的首页。

总结

       以上就是今天博主分享的内容了,本文仅仅简单介绍了Nginx的入门使用,而Nginx的强大之处不仅如此,还有跟多的内容需要大家自己去深入学习。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇

)">
下一篇>>