nginx 1.15.5 的编译安装

  1. 安装环境,下载安装包,编译安装

    # yum install net-tools gcc zlib zlib-devel openssl openssl-devel pcre pcre-devel -y && wget http://nginx.org/download/nginx-1.15.5.tar.gz && tar -zxvf nginx-1.15.5.tar.gz && cd nginx-1.15.5 && ./configure && make && make install && /usr/local/nginx/sbin/nginx
    
    • 可执行以下这条命令,将管理 nginx 的路径写入 PATH 中

      # echo "PATH=\$PATH:/usr/local/nginx/sbin" >> ~/.bash_profile && source ~/.bash_profile
      
  2. 修改配置文件

    # vim /usr/local/nginx/conf/nginx.conf
    
    • 附:开启目录浏览功能

      location / {
          root /home/down; # 需要开启浏览功能的目录
          autoindex on;  # 开启目录文件列表
          autoindex_exact_size off;  # 不显示文件的确切大小,以KB,MB,GB显示
          autoindex_localtime on;  # 显示的文件时间为文件的服务器时间
          charset utf-8,gbk;  # 避免中文乱码
      }
  3. nginx重启

    # /usr/local/nginx/sbin/nginx -s reload
    
    • 若已将管理 nginx 文件的路径写入 PATH,则:

      # nginx -s reload
      

p.s. 有时重启 nginx 的时候会有这样的报错:

[root@ao2 ~]# nginx -s reload
nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
  • 执行以下命令即可:

    # /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    

nginx 的一些简单配置

1. 反向代理

参考https://www.vpser.net/manage/linux-vps-nginx-reverse-proxy.html
介绍
反向代理简单来说就是访问 a.com,而显示的是 b.com 的内容。
配置文件

server
    {
        listen          80;
        server_name     a.com;

        location / {
            proxy_pass              http://b.com/;
            proxy_redirect          off;
            proxy_set_header        X-Real-IP       $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}

2. typecho 伪静态

介绍
伪静态通俗来说就是去掉网址中的 index.php
配置规则

if (!-e $request_filename) {
    rewrite ^(.*)$ /index.php$1 last;
}

附:官方文档中的安装方法

链接:http://nginx.org/en/linux_packages.html

RHEL/CentOS

Install the prerequisites:

sudo yum install yum-utils

To set up the yum repository, create the file named /etc/yum.repos.d/nginx.repo with the following contents:

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key

By default, the repository for stable nginx packages is used. If you would like to use mainline nginx packages, run the following command:

sudo yum-config-manager --enable nginx-mainline

To install nginx, run the following command:

sudo yum install nginx

When prompted to accept the GPG key, verify that the fingerprint matches 573B FD6B 3D8F BC64 1079 A6AB ABF5 BD82 7BD9 BF62, and if so, accept it.