CentOS7下的Nginx安装和配置

Nginx是目前最流行的网站服务器软件,可以说是服务端必备工具。相比传统的Apache服务器软件,Nginx更轻量化,性能更好。
以下教程按照阿里云centOS7服务器为参考。

为yum添加库

sudo yum install epel-release

使用yum安装

sudo yum install nginx

启动Nginx

sudo systemctl start nginx

然后通过IP地址或域名可以访问站点,默认是打开CentOS介绍页。

修改网站文件目录

打开/etc/nginx/nginx.conf文件,http{}server{}里的这句说明了默认的网站文件目录位置。
root /usr/share/nginx/html;

我们可以直接修改到实际放置网站文件的目录。比如
root /10knet/web;
修改后需要重新启动Nginx使其生效。

重新启动

nginx -s reload
重启后通过IP地址或域名可以访问站点,检查是否成功。

新增配置文件

nginx.conf是Nginx的最基本设置文件,它也规定了还要自动载入哪些别的配置文件,默认情况会自动载入下面两个目录下所有的.conf文件,第二个include在http{}里面。

include /usr/share/nginx/modules/*.conf;
include /etc/nginx/conf.d/*.conf;

如果你的网站文件夹很固定,比如一直是/10knet/这个文件,那么就可以把把它也添加到http{}中来:

    # 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.
    include /etc/nginx/conf.d/*.conf;
    include /10knet/*.conf;

然后就可以在网站文件夹下创建一个myNginx.conf文件,把整个server{}内容剪切、粘贴过去,保存好之后重启使配置生效。

这样做的好处是以后可以直接在项目目录内修改Nginx配置,而不需要每次都打开Nginx目录。缺点是配置被分散开来,要注意不能互相冲突。

下面是nginx.conf的内容:

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

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;

    include /etc/nginx/conf.d/*.conf;
    include /10knet/*.conf;
}

注意其中error_page两段,这是自动处理找不到页面或服务器出错的,遇到这种情况服务器就会从这里设定的目录读取40x.html返回给用户,就是最常见的404错误。如果ai.10knet.com/333找不到文件,就会自动返回10knet/pub/ai/err/40x.html文件。

代理转发

除了静态网页文件服务,我们还要接收和反应用户接口的请求服务,比如用户请求/login的时候服务器要进行用户密码验证。

因为我们的接口处理程序都是运行在服务器某个端口上的,所以我们只要让Nginx把这些接口请求转发到这个端口就可以了。

配置代码如下:

 location ^~ /api/ {
        proxy_pass http://127.0.0.1:3100/api/; 
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header Via    "nginx";        
    }

这是把所有/api/...接口都转发到http://127.0.0.1:3100/api/。所以我们必须编写和运行一个服务器程序运行在3100端口上,比如用Java、Nogdejs或者Golang编写都可以,实际上也非常简单。

重点提示!务必设置阿里云服务器的端口开放权限,禁止3100端口对外开放,不能让用户直接访问这个端口,否则很容易造成安全风险。

子域名配置

子域名就是类似http://app.10knet.com或者http://ai.10knet.com这样的域名。可以让我们把网站的不同模块内容清楚地划分开。

可以在阿里云DNS云解析设置中为网站配置特定的子域名,但那要指定不同的服务器,而且要手工为每个子域名进行配置。

能不能在一个服务器上自动配置所有子域名?这就是Nginx的泛域名解析模式。方法是采样动态的server_name,比如myNginx.conf中下面这个写法:

server_name  ~^(?.+).10knet.com$;
root         /10knet/pub/$subdomain/; 
index  index.html index.htm;

这就可以接收任意的xxx.10knet.com的域名地址,并且自动匹配到/10knet/pub/xxx文件夹下的index.html页面。

如下所示的是/10knet/myNginx.conf文件内容:

server {
    #http主要泛域名服务器,静态文件和接口
    listen       80;
    server_name  ~^(?.+).10knet.com$;
    root         /10knet/pub/$subdomain/; 
    index  index.html index.htm;
    
    #接口代理到本地服务程序
    location ^~ /api/ {
        proxy_pass http://127.0.0.1:3100/api/; 
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header Via    "nginx";        
    }
    
    #出错地址指向子域名内error文件夹
    error_page 404 /40x.html;
    location = /40x.html {
        root /10knet/pub/$subdomain/err ;
    }
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /10knet/pub/$subdomain/err;
    }    
}

重定向设置

上面设置了泛域名,但丢失了主域名10knet.com的解析,我们偷个懒,直接把它重定向到www.10knet.com,增加一个新的server配置就可以,代码如下:

server {
    #重定向http://10knet.com到http://www.10knet.com
    listen       80;
    server_name  10knet.com;
    return 301 http://www.10knet.com$request_uri;
}

设置为随系统启动

使用下面的命令让Nginx自动开机启动,可以使用下面的命令。

sudo systemctl enable nginx

也可以直接修改/etc/rc.d/rc.local命令,例如下面这样:

# 启动MongoDB
nohup mongod --dbpath=/var/lib/mongo --logpath=/var/log/mongodb/log.txt > /shell/mongod.log 2>&1 &
sleep 3s
sudo systemctl start nginx
sleep 3s
# 启动其他程序
(
  cd /opt/app
  nohup ./app > kfission.log 2>&1 &
  cd /
)

关于这个代码请参考这个文章【编程】Golang服务端程序部署


欢迎关注我的专栏( つ•̀ω•́)つ【人工智能通识】

每个人的智能新时代

如果您发现文章错误,请不吝留言指正;
如果您觉得有用,请点喜欢;
如果您觉得很有用,欢迎转载~

你可能感兴趣的:(CentOS7下的Nginx安装和配置)