nginx 开发_NGINX开发人员简介

nginx 开发

by Stefanos Vardalos

由Stefanos Vardalos

NGINX开发人员简介 (An Introduction to NGINX for Developers)

Picture this - you’ve created a web application and are now searching for the right web server to host it from.

想象一下-您已经创建了一个Web应用程序,现在正在寻找合适的Web服务器来托管它。

Your application might consist of multiple static files — HTML, CSS, and JavaScript, a backend API service or even multiple webservices. Using Nginx might be what you are looking for, and there are couple of reasons for that.

您的应用程序可能包含多个静态文件-HTML,CSS和JavaScript,一个后端API服务甚至多个Web服务。 您正在寻找使用Nginx的原因,这有几个原因。

NGINX is a powerful web server and uses a non-threaded, event-driven architecture that enables it to outperform Apache if configured correctly. It can also do other important things, such as load balancing, HTTP caching, or be used as a reverse proxy.

NGINX是一个功能强大的Web服务器,并使用非线程的,事件驱动的体系结构,如果配置正确,它可以胜过Apache。 它还可以做其他重要的事情,例如负载平衡,HTTP缓存或用作反向代理。

In this article, I’ll cover a few basic steps about how to install and configure the most common parts of NGINX.

在本文中,我将介绍一些有关如何安装和配置NGINX最常见部分的基本步骤。

基本安装-体系结构 (Basic Installation — Architecture)

There are two ways to install NGINX, either using a pre-built binary or building it up from source.

有两种安装NGINX的方法,一种是使用预构建的二进制文件,也可以是从源代码构建它。

The first method is much easiest and faster, but building it up from source provides the ability to include various third-party modules that make NGINX even more powerful. It allows us to customize it to fit the needs of the application.

第一种方法最简单,最快捷,但是从源代码构建它可以包含各种第三方模块,从而使NGINX更加强大。 它允许我们自定义它以适合应用程序的需求。

To install a prebuilt Debian package, the only thing you have to do is:

要安装预构建的Debian软件包,您唯一要做的就是:

sudo apt-get updatesudo apt-get install nginx

After the installation process has finished, you can verify everything is OK by running the command below, which should print the latest version of NGINX.

安装过程完成后,您可以通过运行以下命令来验证一切正常,该命令应打印最新版本的NGINX。

sudo nginx -vnginx version: nginx/1.6.2

Your new webserver will be installed at the location /etc/nginx/. If you go inside this folder, you will see several files and folders. The most important ones that will require our attention later are the file nginx.conf and the folder sites-available.

您的新Web服务器将安装在/etc/nginx /位置。 如果进入该文件夹,将看到多个文件和文件夹。 稍后需要我们注意的最重要的是文件nginx.conf和文件夹sites-available

配置设定 (Configuration Settings)

The core settings of NGINX are in the nginx.conf file, which by default looks like this.

NGINX的核心设置位于nginx.conf文件中,默认情况下如下所示。

user www-data;worker_processes 4;pid /run/nginx.pid;events {	worker_connections 768;	# multi_accept on;}http {	sendfile on;	tcp_nopush on;	tcp_nodelay on;	keepalive_timeout 65;	types_hash_max_size 2048;	# server_tokens off;	# server_names_hash_bucket_size 64;	# server_name_in_redirect off;	include /etc/nginx/mime.types;	default_type application/octet-stream;	access_log /var/log/nginx/access.log;	error_log /var/log/nginx/error.log;	gzip on;	gzip_disable "msie6";	# gzip_vary on;	# gzip_proxied any;	# gzip_comp_level 6;	# gzip_buffers 16 8k;	# gzip_http_version 1.1;	# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;	include /etc/nginx/conf.d/*.conf;	include /etc/nginx/sites-enabled/*;}

The file is structured into Contexts. The first one is the events Context, and the second one is the http Context. This structure enables some advanced layering of your configuration as each context can have other nested contexts that inherit everything from their parent but can also override a setting as needed.

该文件被构造为Contexts 。 第一个是事件上下文,第二个是http上下文。 这种结构可以使您的配置具有一些高级层次,因为每个上下文可以具有其他嵌套上下文,这些嵌套上下文从其父级继承所有内容,但也可以根据需要覆盖设置。

Various things in this file can be tweaked based on your needs, but NGINX is so simple to use that you can go along even with the default settings. Some of the most important pieces of the NGINX config file are:

该文件中的各种内容都可以根据您的需要进行调整,但是NGINX的使用非常简单,即使使用默认设置也可以使用。 NGINX配置文件中最重要的部分包括:

  • worker_processes: This setting defines the number of worker processes that NGINX will use. Because NGINX is single threaded, this number should usually be equal to the number of CPU cores.

    worker_processes:此设置定义NGINX将使用的工作进程数。 因为NGINX是单线程的,所以该数目通常应等于CPU内核的数目。

  • worker_connections: This is the maximum number of simultaneous connections for each worker process and tells our worker processes how many people can simultaneously be served by NGINX. The bigger it is, the more simultaneous users the NGINX will be able to serve.

    worker_connections:这是每个工作进程同时进行连接的最大数量,它告诉我们的工作进程NGINX可以同时为多少人提供服务。 它越大,NGINX将能够同时服务的用户越多。

  • access_log & error_log: These are the files that NGINX will use to log any erros and access attempts. These logs are generally reviewed for debugging and troubleshooting.

    access_log和error_log:这些是NGINX将用于记录任何错误和访问尝试的文件。 通常会检查这些日志以进行调试和故障排除。

  • gzip: These are the settings for GZIP compression of NGINX responses. Enabling this one along with the various sub-settings that by default are commented out will result in a quite a big performance upgrade. From the sub-settings of GZIP, care should be taken for the gzip_comp_level, which is the level of compression and ranges from 1 to 10. Generally, this value should not be above 6 — the gain in terms of size reduction is insignificant, as it needs a lot more CPU usage. gzip_types is a list of response types that compression will be applied on.

    gzip:这些是NGINX响应的GZIP压缩设置。 启用此选项以及默认情况下已注释掉的各种子设置,将导致相当大的性能提升。 从GZIP的子设置中,应注意gzip_comp_level,它是压缩级别,范围为1到10。通常,此值不应大于6-在减小大小方面,增益微不足道,因为它需要更多的CPU使用率。 gzip_types是将应用压缩的响应类型的列表。

Your NGINX install can support far more than a single website, and the files that define your server’s sites live in the /etc/nginx/sites-available directory.

您的NGINX安装不仅可以支持单个网站,而且定义服务器站点的文件还位于/ etc / nginx / sites-available目录中。

However, the files in this directory aren’t “live” — you can have as many site definition files in here as you want, but NGINX won’t actually do anything with them unless they’re symlinked into the /etc/nginx/sites-enabled directory (you could also copy them there, but symlinking ensures there’s only one copy of each file to keep track of).

但是,此目录中的文件不是“活动”的,您可以在其中拥有任意数量的站点定义文件,但是NGINX实际上不会对其进行任何操作,除非它们被符号链接到/ etc / nginx /中。启用了站点的目录(您也可以将其复制到此处,但是符号链接可确保每个文件只有一个副本可用于跟踪)。

This gives you a method to quickly put websites online and take them offline without having to actually delete any files — when you’re ready for a site to go online, symlink it into sites-enabled and restart NGINX.

这为您提供了一种方法,可以使网站快速上线并使它们脱机,而无需实际删除任何文件-当您准备好使站点上线时,将其符号链接到启用了站点的站点中,然后重新启动NGINX。

The sites-available directory includes configurations for virtual hosts. This allows the web server to be configured for multiple sites that have separate configurations. The sites within this directory are not live and are only enabled if we create a symbolic link into the sites-enabled folder.

sites-available目录包括虚拟主机的配置。 这允许为具有单独配置的多个站点配置Web服务器。 此目录中的网站不是活动的,只有在我们sites-enabledsites-enabled文件夹中创建符号链接后sites-enabled

Either create a new file for you application or edit the default one. A typical configuration looks like the below one.

为您的应用程序创建一个新文件或编辑默认文件。 典型配置如下图所示。

upstream remoteApplicationServer {    server 10.10.10.10;}upstream remoteAPIServer {    server 20.20.20.20;    server 20.20.20.21;    server 20.20.20.22;    server 20.20.20.23;}server {    listen 80;    server_name www.customapp.com customapp.com    root /var/www/html;    index index.html        location / {            alias /var/www/html/customapp/;            try_files $uri $uri/ =404;        }        location /remoteapp {            proxy_set_header   Host             $host:$server_port;            proxy_set_header   X-Real-IP        $remote_addr;            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;            proxy_pass http://remoteAPIServer/;        }        location /api/v1/ {            proxy_pass https://remoteAPIServer/api/v1/;            proxy_http_version 1.1;            proxy_set_header Upgrade $http_upgrade;            proxy_set_header Connection 'upgrade';            proxy_set_header Host $host;            proxy_cache_bypass $http_upgrade;            proxy_redirect http:// https://;        }}

Much like the nginx.conf, this one also uses the concept of nested contexts (and all of these are also nested inside the HTTP context of nginx.conf, so they also inherit everything from it).

就像nginx.conf一样,该nginx.conf也使用了嵌套上下文的概念(而且所有上下文都嵌套在nginx.conf的HTTP上下文中,因此它们也从中继承了所有内容)。

The server context defines a specific virtual server to handle your clients’ requests. You can have multiple server blocks, and NGINX will choose between them based on the listen and server_name directives.

服务器上下文定义了一个特定的虚拟服务器来处理客户的请求。 您可以有多个服务器块,NGINX将根据listenserver_name指令在它们之间进行选择。

Inside a server block, we define multiple location contexts that are used to decide how to handle the client requests. Whenever a request comes in, NGINX will try to match its URI to one of those location definitions and handle it accordingly.

在服务器块内,我们定义了多个位置上下文,这些上下文用于决定如何处理客户端请求。 每当有请求进入时,NGINX都会尝试将其URI与那些位置定义之一进行匹配并进行相应的处理。

There are many important directives that can be used under the location context, such as:

在位置上下文下可以使用许多重要的指令,例如:

  • try_files will try to serve a static file found under the folder that points to the root directive.

    try_files将尝试提供指向根指令的文件夹下的静态文件。

  • proxy_pass will send the request to a specified proxied server.

    proxy_pass会将请求发送到指定的代理服务器。

  • rewrite will rewrite the incoming URI based on a regular expression so that another location block will be able to handle it.

    rewrite将基于正则表达式重写传入的URI,以便另一个位置块将能够处理它。

The upstream context defines a pool of servers that NGINX will proxy the requests to. After we create an upstream block and define a server inside it we can then reference it by name inside our location blocks. Furthermore, an upstream context can have many servers assigned under it so that NGINX will do some load balancing when proxying the requests.

上游上下文定义了一个服务器池,NGINX将把请求代理到该服务器。 创建上游块并在其中定义服务器后,我们可以在位置块中按名称引用它。 此外,上游上下文可以在其下分配许多服务器,以便NGINX在代理请求时进行一些负载平衡。

启动NGINX (Start NGINX)

After we have finished with the configuration and we have moved our web application over to the appropriate folder, we can start up NGINX using the below command:

完成配置并将我们的Web应用程序移至相应的文件夹后,我们可以使用以下命令启动NGINX:

sudo service nginx start

After that, whenever we change something on our configuration, we only have to reload it (without downtime) using the command below.

之后,只要我们更改配置中的任何内容,就只需使用以下命令重新加载它(无停机时间)。

service nginx reload

Lastly, we can check NGINX’s status using the command below.

最后,我们可以使用以下命令检查NGINX的状态。

service nginx status
结论 (Conclusion)

With so many features out of the box, NGINX can be a great way to serve your application or even be used as a HTTP proxy or load balancer for your other web servers. Understanding the way NGINX works and handles requests will give a lot of power to scaling and balancing the load of your applications.

NGINX具有许多现成的功能,可以成为服务您的应用程序的好方法,甚至可以用作其他Web服务器的HTTP代理或负载平衡器。 了解NGINX的工作方式和处理请求的方式将为扩展和平衡应用程序的负载提供很多功能。

翻译自: https://www.freecodecamp.org/news/an-introduction-to-nginx-for-developers-62179b6a458f/

nginx 开发

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值