Nginx安装和常用命令

一、安装

1.官网下载Nginx安装包

Nginx官方下载地址

中文文档地址

2.安装依赖

所需要的的依赖,pcre、OpenSSL、zlib

(1)安装 openssl 、zlib 、 gcc 依赖

# 安装 zlib gcc OpenSSL 依赖
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel

(2)安装pcre

# pcre官网 https://ftp.pcre.org/pub/pcre/
mkdir -p /opt/nginx && cd /opt/nginx &&	wget --no-check-certificate https://netix.dl.sourceforge.net/project/pcre/pcre/8.40/pcre-8.40.tar.gz && tar -zxvf pcre-8.40.tar.gz && cd pcre-8.40 && /opt/nginx/pcre-8.40/configure && make && make install && pcre-config --version

3.安装Nginx

# 下载
cd /opt/nginx && wget http://nginx.org/download/nginx-1.22.1.tar.gz  && tar -zxvf nginx-1.22.1.tar.gz

# 安装
cd /opt/nginx/nginx-1.22.1/ && ./configure --prefix=/opt/nginx/nginx --with-http_stub_status_module --with-http_ssl_module && make && make install && /opt/nginx/nginx/sbin/nginx -v

# 启动(无输出就是启动成功)
/opt/nginx/nginx/sbin/nginx
# 查看是否启动
ps -ef | grep nginx
# 永久开启端口(nginx默认为80端口)
firewall-cmd --add-port= 80/tcp --permanent
# 重启防火墙
firewall-cmd --reload
# 测试访问 
curl 0.0.0.0

配置环境变量

vim /etc/profile.d/my_env.sh

# NGINX_HOME
export NGINX_HOME=/opt/nginx/nginx/
export PATH=$PATH:$NGINX_HOME/sbin

刷新:source /etc/profile

二、常用命令

# 查看构建参数
nginx -V

# 帮助
nginx -h

# 启动nginx
nginx

# 关闭nginx
nginx -s stop

# 重新加载nginx配置文件(无需重启服务,加载配置信息)
nginx -s reload

# 验证配置文件语法是否正确/找到nginx使用的配置文件
nginx -t

# 指定启动的配置文件
nginx -c 配置文件

三、nginx 安装echo调试模块

参考:https://blog.csdn.net/Liang_GaRy/article/details/118229291

# 查看编译指令
nginx -V

# 下载
wget https://github.com/openresty/echo-nginx-module/archive/refs/tags/v0.63.tar.gz

# 解压
tar zxvf echo-nginx-module-0.63.tar.gz

# 如果之前已经编译过nginx,那么就删除: rm -rf /opt/nginx/nginx

# 重新编译并安装到/opt/nginx/nginx路径下
cd /opt/nginx/nginx-1.22.1
./configure --prefix=/opt/nginx/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=../echo-nginx-module-0.63
make && make install

# 查看echo模块是否添加成功
nginx -V

# 测试
vim /opt/nginx/nginx/conf/nginx.conf
# 添加内容
    location / {
        echo "这是默认的 $request_uri";
    }
    location ~* \.(jpg|png) {
        echo "这是~* \.(jpg|png)";
    }
    location ~ \.(jpg|png) {
        echo "这是大小写敏感的~ \.(jpg|png)";
    }
    location ^~ /haha/  {
        echo "这是只要匹配上就停止的 ^~ /haha/";
    }
    location = /haha/liangjiawei.png  {
        echo "这是只要绝对相等的 = /haha/liangjiawei.png ";
    }
# 执行
curl 0.0.0.0
>> 这是默认的 /

四、配置实例

# 指定启动用户
user  root;
worker_processes  2;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {

        error_page   500 502 503 504  /50x.html;

        # 当前服务块(虚拟主机)监听的端口号,不指定网卡ip,则监听全部网卡
        listen       80;
        # 客户端浏览器填写的域名,用来区分服务的(多个server块之间必须满足listen与server_name不能同时相同)
        server_name  localhost;

        # 请求 http://localhost:80/ 时就返回下方index映射的资源
        # 指资源的文件夹路径
        location / {
        # 指定匹配该location请求的寻找资源的上下文路劲,默认是nginx安装目录的html
        root   html;
        # 指定请求 location / 时默认返回的文件
        index  index.html index.htm; # index可匹配html文件夹中的index.html文件与index.htm文件
        }

        # web应用常用此配置
        location /zhgsgl/ {
            root   /root/www/;
            # try_files指令说明:https://blog.csdn.net/zhuyongxin_6688/article/details/121408216
            try_files $uri $uri/ /index.html;
        }

    	# 相对路径时:指在nginx安装目录根路径下的html文件夹绝对匹配50x.html文件
        location = /50x.html {
        root   html;
        }

        # 反向代理 http://www.baidu.com/ 服务
        # 请求 http://127.0.0.1:80/baidu/ 时跳转至 http://www.baidu.com/
        # / 很重要不可缺失
        location /baidu/ {
            proxy_pass   http://www.baidu.com/;
        }

        # 反向代理 http://192.168.1.5:9000/ 服务
        # 请求 http://127.0.0.1:80/proxy/ 时跳转至 http://192.168.1.5:9000/
        # / 很重要不可缺失
        location /proxy/ {
            proxy_pass   http://192.168.1.5:9000/;
        }

        # 静态文件服务
        # 请求 http://localhost:80/file 时,以 /home/jia/file/ 为顶层目录,递归的返回内部所有文件 
        location /file/ {
            root  /home/jia/;
            # 自动索引 root键 指定的的值的文件夹中的文件
            autoindex on;
        }

        # 返回字符串配置
        location /debug/ {
            add_header Content-Type 'text/html; charset=utf-8';
            return 200 '恭喜你、请求成功';
        }
    }
}

参考文章

开启ssl功能

posted @ 2021-03-11 01:33  黄河大道东  阅读(146)  评论(0编辑  收藏  举报