How to install and configure Nginx

PHPz
Release: 2023-05-12 14:17:21
forward
1287 people have browsed it

简介

Nginx 的安装:

# CentOS
yum install nginx;
# Ubuntu
sudo apt-get install nginx;
# Mac
brew install nginx;
Copy after login

一般可以在/etc/nginx/nginx.conf中配置,启动参数为:

# 启动
nginx -s start;
# 重新启动,热启动,修改配置重启不影响线上
nginx -s reload;
# 关闭
nginx -s stop;
# 修改配置后,可以通过下面的命令测试是否有语法错误
nginx -t;
Copy after login

-s,signal,意思就是向 nginx 发送start|reload|stop命令,还是很好理解的。先看一个最简单的nginx.conf配置:

events {
    # 需要保留这一个段落,可以为空
}
http {
    server {
        listen 127.0.0.1:8888;
        location / {
            root /home/chenya/test/;
        }
    }
}
Copy after login

启动后,访问htttp://127.0.0.1:8888,如果/home/chenya/test/下有index.html文件就会展示index.html的内容,否则返回404

Nginx 配置一个 Web 服务器

以下对配置 Web 服务器的参数做简单说明,包括如何配置端口、域名,如何处理请求,如何响应请求。

1、 虚拟主机和请求的分发

域名和端口的配置

listen 127.0.0.1:8000;
listen *:8000;
listen localhost:8000;
# IPV6
listen [::]:8000;
# other params
listen 443 default_serer ssl;
listen 127.0.0.1 default_server accept_filter=dataready backlog=1024
Copy after login

主机名配置

server_name www.chenya.site  chenya.site
server_name *.chenya.com
server_name ~^\.chenya\.com$
Copy after login

URI 匹配

location = / {
    # 完全匹配  =
    # 大小写敏感 ~
    # 忽略大小写 ~*
}
location ^~ /images/ {
    # 前半部分匹配 ^~
    # 可以使用正则,如:
    # location ~* \.(gif|jpg|png)$ { }
}
location / {
    # 如果以上都未匹配,会进入这里
}
Copy after login

2、 文件路径的定义

根目录设置

location / {
    root /home/chenya/test/;
}
Copy after login

别名设置

location /blog {
    alias /home/chenya/www/blog/;
}
location ~ ^/blog/(\d+)/([\w-]+)$ {
    # /blog/20141202/article-name 
    # -> /blog/20141202-article-name.md
    alias /home/chenya/www/blog/$1-$2.md;
}
Copy after login

首页设置

index /html/index.html /php/index.php;
Copy after login

重定向页面设置

error_page    404         /404.html;
error_page    502  503    /50x.html;
error_page    404  =200   /1x1.gif;
location / {
    error_page  404 @fallback;
}
location @fallback {
    # 将请求反向代理到上游服务器处理
    proxy_pass http://localhost:9000;
}
Copy after login

try_files 设置

try_files $uri $uri.html $uri/index.html @other;
location @other {
    # 尝试寻找匹配 uri 的文件,失败了就会转到上游处理
    proxy_pass  http://localhost:9000;
}
location / {
    # 尝试寻找匹配 uri 的文件,没找到直接返回 502
    try_files $uri $uri.html =502;
}
Copy after login

Nginx 配置反向代理服务器

反向代理(reserve proxy)方式是指用代理服务器来接受 Internet 上的连接请求,然后将请求转发给内部网络中的上游服务器,并将上游服务器上得到的结果返回给 Internet 上请求连接的客户端,此时代理服务器对外的表现就是一个 Web 服务器。

Nginx 具备超强的高并发高负载能力,一般会作为前端的服务器直接向客户端提供静态文件服务;而业务一般还包含一些业务逻辑需要 Apache、Tomcat 等服务器来处理,故通常 Nginx 对外表现即为静态 Web 服务器也是反向代理服务器。

缺点是增加了一次请求的处理时间,优点是降低了上游服务器的负载,尽量将压力放在 Nginx 服务器上。

1、负载均衡配置

upstream,定义一个上游服务器集群

upstream backend {
    # ip_hash;
    server s1.chenya.com;
    server s2.chenya.com;
}
server {
    location / {
        proxy_pass http://backend;
    }
}
Copy after login

2、反向代理

proxy_pass 将请求转发到有处理能力的端上,默认不会转发请求中的 Host 头部

location /blog {
    prox_pass http://localhost:9000;
    ### 下面都是次要关注项
    proxy_set_header Host $host;
    proxy_method POST;
    # 指定不转发的头部字段
    proxy_hide_header Cache-Control;
    proxy_hide_header Other-Header;
    # 指定转发的头部字段
    proxy_pass_header Server-IP;
    proxy_pass_header Server-Name;
    # 是否转发包体
    proxy_pass_request_body on | off;
    # 是否转发头部
    proxy_pass_request_headers on | off;
    # 显形/隐形 URI,上游发生重定向时,Nginx 是否同步更改 uri
    proxy_redirect on | off;
}
Copy after login

一个简单的例子,Node.js

一个十分常见的需求:处理请求,如果是静态文件,Nginx 直接返回,否则交给 Node 服务器处理。首先创建了一个 Node 服务器:

const http = require('http');
http.createServer((req, res) => {
    res.end('hello world');
}).listen(9000);
Copy after login

任何请求过来都返回hello world,简版的 Nginx 配置如下,

events {
    # 这里可不写东西
    use epoll;
}
http {
    server {
        listen 127.0.0.1:8888;
        # 如果请求路径跟文件路径按照如下方式匹配找到了,直接返回
        try_files $uri $uri/index.html;
        location ~* ^/(js|css|image|font)/$ {
            # 静态资源都在 static 文件夹下
            root /home/chenya/www/static/;
        }
        location /app {
            # Node.js 在 9000 开了一个监听端口
            proxy_pass http://127.0.0.1:9000;
        }
        # 上面处理出错或者未找到的,返回对应状态码文件
        error_page    404            /404.html;
        error_page    502  503  504  /50x.html;
    }
}
Copy after login

首先 try_files,尝试直接匹配文件;没找到就匹配静态资源;还没找到就交给 Node 处理;否则就返回 4xx/5xx 的状态码。

测试语法

nginx -t
Copy after login

The above is the detailed content of How to install and configure Nginx. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!