微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Linux Nginx 下载 安装 配置 使用

博文目录


环境

阿里云ECS(Aliyun Linux 2.1903 LTS 64位)

下载

http://Nginx.org/en/download.html
http://Nginx.org/download/Nginx-1.18.0.tar.gz

选择稳定版(Stable version)下载即可,分 linux 和 windows 两种,linux 版下载的是源码,源码放到临时目录,然后编译安装到指定目录

安装

tar -zxvf Nginx-1.18.0.tar.gz 得到解压包,直接在解压包内执行下面的命令,注意安装路径需要手动创建,不要直接装在解压后的路径内
./configure --help 获取可配置参数
./configure --prefix=/mrathena/application/Nginx-1.18.0 配置安装目录,该目录会自动创建,执行之后会打印出配置信息
./configure --prefix=/mrathena/application/Nginx-1.18.0 --with-http_ssl_module 如果需要使用 https(ssl) 的话,需要添加该配置,执行之后会打印出配置信息

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  Nginx path prefix: "/mrathena/application/Nginx-1.18.0"
  Nginx binary file: "/mrathena/application/Nginx-1.18.0/sbin/Nginx"
  Nginx modules path: "/mrathena/application/Nginx-1.18.0/modules"
  Nginx configuration prefix: "/mrathena/application/Nginx-1.18.0/conf"
  Nginx configuration file: "/mrathena/application/Nginx-1.18.0/conf/Nginx.conf"
  Nginx pid file: "/mrathena/application/Nginx-1.18.0/logs/Nginx.pid"
  Nginx error log file: "/mrathena/application/Nginx-1.18.0/logs/error.log"
  Nginx http access log file: "/mrathena/application/Nginx-1.18.0/logs/access.log"
  Nginx http client request body temporary files: "client_body_temp"
  Nginx http proxy temporary files: "proxy_temp"
  Nginx http fastcgi temporary files: "fastcgi_temp"
  Nginx http uwsgi temporary files: "uwsgi_temp"
  Nginx http scgi temporary files: "scgi_temp"

make install 编译安装,完成后,/mrathena/application/Nginx-1.18.0 目录下将出现 conf,html,logs,sbin 等目录,Nginx 脚本就在 sbin 目录下
Nginx 的编译需要一些依赖库,如果缺少的话,需要手动添加
yum –y install gcc-c++ 编译依赖gcc环境
yum install -y pcre pcre-devel 安装 pcre 库,pcre-devel是使用pcre开发的一个二次开发库
yum install -y zlib zlib-devel 安装 zlib 库,zlib库提供了很多种压缩和解压缩的方式,Nginx使用zlib对Http包内容进行gzip
yum install -y openssl openssl-devel OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用

配置

Nginx 认使用 80 端口,建议对外只暴露 Nginx 和 80 端口,用 Nginx 来反向代理内部服务,安全还方便
参数配置 Nginx configuration file: "/mrathena/application/Nginx-1.18.0/conf/Nginx.conf"

我的应用列表

  • 8888: mrathena.jar
  • 8080: jenkins.war

我的域名: mrathena.cn,需在阿里云域名解析配置A类解析,service和jenkins,或者*

我的目的

server {
    listen 80;
    server_name service.mrathena.cn;
    location / {
        proxy_pass http://localhost:8888;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
server {
    listen 80;
    server_name jenkins.mrathena.cn;
    location / {
        proxy_pass http://localhost:8080;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

使用

./Nginx 启动 Nginx,使用配置文件
./Nginx -s stop 快速停止,相当于直接 kill
./Nginx -s quit 完整停止
./Nginx -s reload 运行时重新加载配置文件
./Nginx -t 验证当前配置文件是否正确可用

可以配置 alias 方便使用,在 /root/.bashrc 文件添加如下 alias,可在任何地方直接执行 Nginx.start,Nginx.reload

alias cdNginx='cd /mrathena/application/Nginx-1.18.0'
alias Nginx.start='/mrathena/application/Nginx-1.18.0/sbin/Nginx'
alias Nginx.stop='/mrathena/application/Nginx-1.18.0/sbin/Nginx -s stop'
alias Nginx.quit='/mrathena/application/Nginx-1.18.0/sbin/Nginx -s quit'
alias Nginx.reload='/mrathena/application/Nginx-1.18.0/sbin/Nginx -s reload'
alias Nginx.check='/mrathena/application/Nginx-1.18.0/sbin/Nginx -t'

参考

www.cnblogs.com/xiaowazi/p/10610508.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐