Nginx 系列2 --- 配置

一、概要

1. 承上启下

(1) Nginx 系列

二、配置

1. 测试配置

sudo nginx -t

2. nginx配置文件

(1) 配置文件

sudo vi /etc/nginx/nginx.conf

(2) 关键配置

events {
  worker_connections  4096;  ## Default: 1024
}
http {
  server {
    listen 80;
    listen [::]:80;
    server_name localhost;
    location / {
      root /etc/nginx/html;
      index index.html index.htm;
    }
  }
}

a. listen: 是nginx需要监听的端口号;

b. server_name: 是nginx代理的域名,如果配置成.example.com,则example.com所有子域名都会被命中;

c. location: 存储路径信息,这里root指http网站的根目录:

请求http://www.example.com/,返回页面对应的目录是/etc/nginx/html/index.html。

3. 子配置文件

(1) 按需创建

sudo vi /etc/nginx/conf.d/http
sudo vi /etc/nginx/conf.d/stream
sudo vi /etc/nginx/conf.d/mail
sudo vi /etc/nginx/conf.d/exchange-enhanced

a. Events: 一般链接处理;

b. Http: Http模块;

c. Mail: 邮件模块;

d. Stream: TCP和UDP流量。

(2) 在/etc/nginx/nginx.conf中按需添加引用

include /etc/nginx/conf.d/http/*.conf;
include /etc/nginx/conf.d/stream/*.conf;
include /etc/nginx/conf.d/exchange-enhanced/*.conf;

4. 配置证书

(1) Letscrypt证书

参考Letscrypt

(2) SELinux

使用自签发证书时,如果服务器开启了SELinux可能会出现Nginx无法加载证书文件的问题,此时需要运行以下命令解决:

sudo restorecon -v -R /etc/nginx/ssl

注意,/etc/nginx/ssl是证书所在的目录。

三、参考

1. 官方

http://nginx.org/en/docs/

http://nginx.org/en/docs/ngx_core_module.html

http://nginx.org/en/docs/http/ngx_http_core_module.html

http://nginx.org/en/docs/http/ngx_http_charset_module.html

http://nginx.org/en/docs/http/ngx_http_log_module.html

http://nginx.org/en/docs/mail/ngx_mail_ssl_module.html

http://nginx.org/en/docs/http/ngx_http_index_module.html

2. 其他

https://www.digitalocean.com/community/tools/nginx

posted @ 2023-12-16 23:13  白马黑衣  阅读(67)  评论(0编辑  收藏  举报