跳转至

NginX基础

NginX基础

更新日期:2020-07-24


1. 概述

NginX是和Apache类似的一个Web服务器软件。非常轻量级, 通常用来解决高并发访问的性能问题。

2. 安装

以在CentOS中安装为例。

在CentOS的repo中已经包含了nginx,因此可以直接使用yum命令来进行安装。

1
yum install nginx

安装完成后,会在系统中安上下面这些东西:

nginx服务

这意味着可以使用systemctl来管理nginx的启动停止等。

一系列配置文件

位于/etc/nginx目录下,这些文件用来配置nginx。 其中nginx.conf为nginx的主配置文件。

可执行命令nginx

使用nginx命令并指定各种选项和参数来执行各种相关的功能。比如nginx -s reload可以重新加载nginx的配置文件。

3. 尝试使用nginx

3.1 启动nginx

使用如下命令可以启动nginx:

1
systemctl start nginx

3.2 访问nginx

nginx启动后,将默认监听80端口,并使用http协议。

直接在能够访问到服务器的PC中访问如下链接即可:

1
http://服务器域名或IP

如果显示NginX的默认页面,则表示nginx已经安装成功。

4. 配置nginx(基础版)

注意:在修改或者添加nginx的时候,可以暂时关闭防火墙,以免防火墙的配置问题和nginx的配置问题纠缠在一起。在nginx配置完成可以成功访问的前提下再打开防火墙,然后设置在防火墙中开放对应的服务和端口。

nginx.conf为主配置文件,我们暂且只关注里面的几个比较重要的配置项。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    #conf.d目录下的.conf文件都会被包含进来,所以能出现在这里的内容都可以出现在那个.conf文件中
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _; # 通常填写希望监听的域名
        root         /usr/share/nginx/html; # 本web服务的根目录,各种页面文件和静态资源等都默认从这里找

        # Load configuration files for the default server block.
        # default.d目录下的.conf文件都会被包含进来,所以能出现在这里的内容都可以出现在那个.conf文件中
        include /etc/nginx/default.d/*.conf;

        location / { # 配置URL映射,URL的配置比较复杂,以后在别的章节种详细介绍。/表示匹配所有
        }

        error_page 404 /404.html; # 配置错误页面
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html; # 配置错误页面
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server. # 配置TLS,希望使用HTTPS时可以这这里配置
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers PROFILE=SYSTEM;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

5. 反向代理tomcat

Nginx只是一个Web服务器,只能够返回各种静态的内容。在实际应用中,通常还需要配合应用服务器来使用,比如tomcat。

要完成这个功能,我们可以把指定域名或URL的访问转发给tomcat,将tomcat的处理结果作为请求的返回。

可以创建一组server配置来添加一组监听。

5.1 为tomcat创建配置文件

由于我们要创建一组server配置,所有要在如下目录创建配置文件。

1
2
/etc/nginx/conf.d
tomcat.conf

在tomcat.conf文件中添加如下内容:(目前只包含一套最精简的配置)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
server {
    listen 80;
    server_name www.hakureiyoumu.com;

    root /var/local/tomcat;

    access_log /var/log/nginx/tomcat_access.log;
    error_log /var/log/nginx/tomcat_error.log;

    location / {
        proxy_pass http://localhost:8080/;
    }

}

5.2 重新加载nginx

使用如下命令重新加载nginx以使新的配置文件生效。

1
nginx -s reload

5.3 验证是否配置成功

访问上面配置的端口和URL,如果成功跳转到tomcat的相关页面,就说明配置成功了。

6. 配置SSL

现在大部分的网站都已经迁移到https,这就需要我们进行ssl的配置。

仍然以转发到tomcat的配置为例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
server {
    listen 443;
    ssl on;
    server_name www.hakureiyoumu.com;

    ssl_certificate /etc/letsencrypt/live/www.hakureiyoumu.xyz/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.hakureiyoumu.xyz/privkey.pem;

    root /var/local/tomcat;

    access_log /var/log/nginx/tomcat_ssl_access.log;
    error_log /var/log/nginx/tomcat_ssl_error.log;

    location / {
        proxy_pass http://localhost:8080/;
    }

}

本例使用的证书文件是使用Let's Encrypt免费认证生成的证书文件。如果想要自己的网站看其实更为正统可靠一点,需要花钱去专门的认证机构申请,会审核各种信息。

一切按照Let's Encrypt网站上的步骤执行就可以了,没什么难度。

配置完后需要重新加载nginx。

1
nginx -s reload

然后你就可以使用https来访问你的网站了。

有时访问会失败,查看错误log会发现报如下错误:

1
connect() to 127.0.0.1:8080 failed (13: Permission denied) while connecting to upstream …

按照网上的说法,是SELinux的原因,详细情形比较复杂。我们只需执行如下命令就可以解决:

1
setsebool -P httpd_can_network_connect 1