Nginx的基本安装配置

前言

参考官网安装教程, 官网上有各个linux发行版中安装nginx的教程, 推荐看官方的教程安装

Centos7使用 yum 安装nginx(几乎是直接翻译官方文档中 通过官方nginx的yum仓库在Centos中安装预编译的nginx 这种安装方式)

  1. /etc/yum.repos.d 目录下创建 nginx.repo 文件在Centos系统建立nginx的 yum 仓库

    $ sudo vim /etc/yum.repos.d/nginx.repo
    
  2. nginx.repo 文件添加如下内容

    [nginx]
    name=nginx repo
    baseurl=https://nginx.org/packages/mainline/<OS>/<OSRELEASE>/$basearch/
    gpgcheck=0
    enabled=1
    

    其中

    • /mainline 元素表示的是 NGINX 开源版的最新主线版本(mainline version), 删除这个元素可以得到最新稳定版(stable version)
    • <OS> 可以是 rhel 也可以是 centos
    • <OSRELEASE>表示系统发行版本的数字, 可以是6 6.x 7 7.x 等

    例如, 要为Centos7安装最新主线版本, 可以像下面这样

    name=nginx repo
    baseurl=https://nginx.org/packages/mainline/centos/7/$basearch/
    gpgcheck=0
    enabled=1
    
  3. 保存并退出编辑器

  4. 更新yum仓库

    $ sudo yum update
    
  5. 安装nginx

    $ sudo yum install nginx
    
  6. 启动nginx

    $ sudo nginx
    
  7. 验证nginx启动成功

    $ curl -l 127.0.0.1
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        body {
             35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
    

升级nginx

yum list |grep nginx 查看列表中存在的稳定版本的nginx

yum update nginx 升级nginx

nginx -s reload 重启nginx

升级可能遇到问题(我没有遇到, 参考的另一篇文章描述的)

可能出现:

nginx: [emerg] module "/usr/lib64/nginx/modules/ngx_http_geoip_module.so" version 1012002 instead of 1014002 in /usr/share/nginx/modules/mod-http-geoip.conf:1

运行下面的命令:

yum remove nginx-mod*

yum install nginx-module-*

检查nginx版本, 确认安装成功

# 查看nginx的版本, 确保安装成功
[root@izuf6ggrfujyiyg1sz1rrzz Laggage]# nginx -v
nginx version: nginx/1.12.2

启动nginx
systemctl start nginx

nginx配置文件

不同版本配置文件位置不同,本文安装的nginx版本在上面是 1.12.2的.
配置文件再 /etc/nginx 目录下

虚拟主机配置

现在nginx的虚拟主机配置文件和nginx的文件是分开的;
配置文件目录: /etc/nginx/nginx.conf
虚拟主机配置文件目录:: /etc/nginx/conf.d

nginx.conf文件中通过 include 命令来引入虚拟主机配置

http{
    ...
    include /etc/nginx/conf.d/*.conf;
    ...
}

虚拟主机配置的例子

文件: /etc/nginx/conf.d/default.conf

server {
    #侦听80端口
    listen       80;
    #定义使用www.xx.com访问
    server_name  www.xx.com;
    #设定本虚拟主机的访问日志
    access_log  logs/www.xx.com.access.log  main;
    #默认请求
    location / {
          root   /root;      #定义服务器的默认网站根目录位置
          index index.php index.html index.htm;   #定义首页索引文件的名称
          fastcgi_pass  www.xx.com;
          fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name; 
          include /etc/nginx/fastcgi_params;
    }
    # 定义错误提示页面
    error_page   500 502 503 504 /50x.html;  
        location = /50x.html {
        root   /root;
    }
    #静态文件,nginx自己处理
    location ~ ^/(images|javascript|js|css|flash|media|static)/ {
        root /var/www/virtual/htdocs;
        #过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
        expires 30d;
    }
    #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.
    location ~ .php$ {
        root /root;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name;
        include fastcgi_params;
    }
    #设定查看Nginx状态的地址
    location /NginxStatus {
        stub_status            on;
        access_log              on;
        auth_basic              "NginxStatus";
        auth_basic_user_file  conf/htpasswd;
    }
    # 配置文件下载服务
    location /download {
        root /www; 
        # 让浏览器打开下载窗口,关键在于attachment,attachment后面不可以加冒号
        add_header Content-Disposition 'attachment';
        add_header Content-Type: 'APPLICATION/OCTET-STREAM';
    }
    #禁止访问 .htxxx 文件
    location ~ /.ht {
        deny all;
    }
 }
}

ps
已=开头表示精确匹配
如 A 中只匹配根目录结尾的请求,后面不能带任何字符串。
^~ 开头表示uri以某个常规字符串开头,不是正则匹配
~ 开头表示区分大小写的正则匹配;
~* 开头表示不区分大小写的正则匹配
/ 通用匹配, 如果没有其它匹配,任何请求都会匹配到
匹配优先级

(location =) > (location 完整路径) > (location ^~ 路径) (location ,* 正则顺序) > (location 部分起始路径) > (/)

配置文件中可以用的全局变量

变量符号 说明
$args 这个变量等于请求行中的参数,同$query_string
$content_length 请求头中的Content-length字段。
$content_type 请求头中的Content-Type字段。
$document_root 当前请求在root指令中指定的值。
$host 请求主机头字段,否则为服务器名称。
$http_user_agent 客户端agent信息
$http_cookie 客户端cookie信息
$limit_rate 这个变量可以限制连接速率。
$request_method 客户端请求的动作,通常为GET或POST。
$remote_addr 客户端的IP地址。
$remote_port 客户端的端口。
$remote_user 已经经过Auth Basic Module验证的用户名。
$request_filename 当前请求的文件路径,由root或alias指令与URI请求生成。
$scheme HTTP方法(如http,https)。
$server_protocol 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
$server_addr 服务器地址,在完成一次系统调用后可以确定这个值。
$server_name 服务器名称。
$server_port 请求到达服务器的端口号。
$request_uri 包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。
$uri 不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。
$document_uri 与$uri相同。

常用正则

&nbsp &nbsp
. 匹配除换行符以外的任意字符
? 重复0次或1次
+ 重复1次或更多次
* 重复0次或更多次
d 匹配数字
^ 匹配字符串的开始
$ 匹配字符串的介绍
{n} 重复n次
{n,} 重复n次或更多次
[c] 匹配单个字符c
[a-z] 匹配a-z小写字母的任意一个

小括号()之间匹配的内容,可以在后面通过$1来引用,$2表示的是前面第二个()里的内容。正则里面容易让人困惑的是反斜杠: 表示转义特殊字符。

原文地址:https://www.cnblogs.com/Laggage/p/11660853.html