关于nginx:Linux下Nginx相关常用操作

4次阅读

共计 2953 个字符,预计需要花费 8 分钟才能阅读完成。

nginx 作为应用最为宽泛的 http 服务器,本文零碎介绍了其在 Linux 的最罕用操作,为大家提供了相干性能速查手册。内容次要包含了,日常操作、常见配置、常见谬误、相干资源三局部内容。

  • Nginx 整体介绍
  • 本文相干前提条件
  • Nginx 日常操作

    • 通过软件源进行装置
    • 启动与进行

      • 查看 nginx 运行日志
  • Nginx 常见配置

    • 一般性操作
    • 配置文件构造
    • 主配置文件
    • https 服务反向代理本地 JAVA 利用示例
  • 相干学习资源

Nginx 整体介绍

NGINX 是一个收费、开源、高性能的 HTTP 服务器和反向代理,以及 IMAP/POP3 代理服务器。NGINX 以其高性能、稳定性、丰盛的功能集、简略的配置和低的资源耗费而闻名。

本文相干前提条件

本文所有操作指令均以曾经取得系统管理员权限为前提。因为不同的 Linux 发行版应用的软件依赖管理工具、零碎服务配置工具差别较大,须要依据不同操作系统抉择相应的指令。

其中不同发行版软件依赖管理工具映射关系如下:

  • yum,实用于 Centos7

其中不同发行版应用的服务管理工具映射关系如下:

  • systemctl,实用于 Centos7

Nginx 日常操作

通过软件源进行装置

  1. 增加 EPEL 软件源,如果曾经装置 EPEL 仓库请跳过该步骤。

yum

yum install epel-release
  1. 装置 Nginx 软件

yum

yum install nginx

启动与进行

  1. 设置开机启动

systemctl

systemctl enable nginx
  1. 启动 nginx

systemctl

systemctl start nginx
  1. 进行 nginx

systemctl

systemctl stop nginx
  1. 查看以后状态

systemctl

systemctl status nginx
  1. 重启 nginx

systemctl

systemctl restart nginx
  1. 从新加载配置

systemctl

systemctl reload nginx

查看 nginx 运行日志

nginx 的默认日志门路配置为 /var/log/nginx/,该章节默认指令操作目录为日志门路。依据日志的操作目标不同,个别会应用 cat、grep、tail 等命令查看日志,该章节默认查看操作为实时滚动查看。

  1. 查看拜访日志
tail -f access.log
  1. 查看谬误日志
tail -f error.log

Nginx 常见配置

一般性操作

在进行配置文件改变后,须要应用执行从新加载使得配置信息失效,还有多数状况须要重新启动 nginx 服务。

配置文件构造

  • 个别 nginx 相干的配置文件都在 /etc/nginx/ 目录中。
  • 主配置文件为上述目录的 nginx.conf,其中定义了 nginx 的默认配置信息。
  • 依据主配置文件默认定义,每一个独立的 nginx 服务配置文件应放在配置目录的 conf.d/ 子目录当中。
  • 个别子配置文件以域名进行命名不便运维人员检索,例如 dm2box.com.conf。

主配置文件

主配置文件作用次要为定义软件公共配置信息,包含日志门路配置、子配置文件配置等要害信息。
/etc/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 4096;

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

    # 加载服务器子配置文件
    include /etc/nginx/conf.d/*.conf;
}

https 服务反向代理本地 JAVA 利用示例

该示例是 JAVA 网站的典型配置示例,其中包含了反向代理配置、http 全副重定向至本域 https 服务、SSL 证书与平安配置等要害配置。
/etc/nginx/conf.d/dm2box.com.conf

# 代理节点配置
upstream dm2box {
    # 本地 dm2box 服务 java 应用服务端口
    server 127.0.0.1:8080;
}

# 80 端口服务
server {
    listen       80;
    server_name  dm2box.com;
    # 全副重定向至 https 协定
    return 301 https://$server_name$request_uri;
}

# 443 端口服务
server {
    listen 443 ssl;
    listen [::]:443;
    server_name dm2box.com;
    client_max_body_size 1024m;
    # ssl 证书地址
    ssl_certificate         /dm2box/full_chain.pem;  
    # 私钥地址
    ssl_certificate_key     /dm2box/private.key; 
    # 缓存有效期
    ssl_session_timeout  5m;
    # 加密算法
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    # 平安链接可选的加密协议
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    # 应用服务器端的首选算法
    ssl_prefer_server_ciphers on;
    #Gzip 相干
    gzip on;
    gzip_buffers 4 16k;
    gzip_comp_level 6;
    gzip_vary on;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;   
    location / {
        # 反向代理配置
        proxy_pass http://dm2box;
        proxy_set_header HOST $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

相干学习资源

  • 官方网站知识库,https://www.nginx.com/resources/wiki/

版权申明,本文首发于 数字魔盒 https://www.dm2box.com/ 欢送转载。

正文完
 0