linux环境nginx安装及使用

nginx

安装

1、安装相关插件

一次安装

linux 复制代码
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel

单独安装(一次安装有问题时可使用)

复制代码
yum install gcc-
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel

作用

  1. gcc 可以编译 C,C++,Ada,Object C和Java等语言(安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境)

  2. pcre pcre-devel pcre是一个perl库,包括perl兼容的正则表达式库,nginx的http模块使用pcre来解析正则表达式,所以需要安装pcre库

  3. zlib zlib-devel zlib库提供了很多种压缩和解压缩方式nginx使用zlib对http包的内容进行gzip,所以需要安装

  4. openssl openssl-devel OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。

    5.nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库

2、安装nginx

方案1、下载好压缩包然后上传

nginx下载地址

linux 复制代码
https://nginx.org/en/download.html

上传压缩包

linux 复制代码
scp -r  /Users/cc/Downloads/xxx root@xxx:/usr/local/share

方案2、使用wget命令下载

下载wget

复制代码
yum install wget

下载nginx

linux 复制代码
wgte https://nginx.org/download/nginx-1.21.6.tar.gz

3、解压安装包

复制代码
tar xvf nginx-1.21.6.tar.gz
cd nginx-1.21.6

4、配置

linux 复制代码
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

5、编译

linux 复制代码
make

6、安装

linux 复制代码
make install

7、查看安装路径

linux 复制代码
whereis nginx

常用命令

开启防火墙、关闭防火墙、查看防火墙状态

linux 复制代码
systemctl start firewalld.service 
systemctl stop firewalld.service 
systemctl status firewalld.service 

启动

linux 复制代码
/usr/local/nginx/sbin/nginx

重启

linux 复制代码
/usr/local/nginx/sbin/nginx -s reload

查看进程

linux 复制代码
ps aux | grep nginx

配置

基本配置

linux 复制代码
server {
  listen       80; #端口号
  server_name  localhost; #域名
  location / {
      root   html;
      index  index.html index.htm;
  }
}
server {
  listen       8082; #端口号
  server_name  localhost; #域名
  location / {
      root   html;
      index  index.html index.htm;
  }
}

vue项目history模式的部署

hash模式需要部署,直接项目放入html中即可

linux 复制代码
server {
  # 部署项目的地址
  location /demo{
    root html;
    index index.html index.htm;
    try_files $uri $uri/ /demo/index.html
  }
  # 反向代理请求,解决跨域问题
  location /Api{
    proxy_pass https://xxx:8088;
  }
}

http 重定向https

linux 复制代码
server {
    listen 80;
    server_name localhost;
    #将请求转成https
    rewrite ^(.*) https://$server_name$1 permanent;
}

https配置

linux 复制代码
    server {
        listen       443 ssl;
        server_name  localhost;
 
        ssl_certificate      cert.pem;#根证书地址(默认把证书放在conf目录)
        ssl_certificate_key  cert.key;#证书秘钥(默认把证书放在conf目录)
 
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
 
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
 
        location / {
            root   html;
            index  index.html index.htm;
        }
    }
相关推荐
NineData2 分钟前
NineData 数据库 DevOps 正式支持谷歌云,全面接入 GCP 数据源
运维·数据库·devops·ninedata·gcp·玖章算术·数据智能管理平台
从零开始学习人工智能3 分钟前
《8076 能通 9003 却超时?一次 Docker 容器跨网段排障小记》
运维·docker·容器
A13247053123 分钟前
Linux文件查找:find和locate命令入门
linux·运维·服务器·网络·chrome
物联网软硬件开发-轨物科技3 分钟前
【轨物新闻】轨物科技光伏清洁机器人智能控制和运维解决方案正式上线华为鲲鹏解决方案市场
运维·科技·机器人
一周困⁸天.4 分钟前
DevOps
运维·devops
飞机火车巴雷特4 分钟前
PyCharm通过跳板机连接隔离内网服务器
运维·服务器
yenggd8 分钟前
华为+sr-mpls BE简单配置案例
运维·网络·华为
xiejava10181 小时前
为了管好IP我上了一套开源的IP管理系统phpIPAM
运维·安全·开源·网管
weixin_436525072 小时前
Linux 终端下的 My Sql 常用操作指南(替代 Navicat)
linux·运维·服务器
上海蓝色星球9 小时前
迈向智慧电网新纪元:上海蓝色星球数字孪生变电主子站系统
运维·数据库