linux下安装nginx

本文详细介绍了在Linux环境下安装Nginx的过程,包括下载Nginx及必要依赖zlib和pcre,配置并编译安装Nginx,以及如何运行和检查Nginx是否成功启动。此外,还提供了修改Server信息的方法,以增强安全性,以及如何进行简单的负载均衡配置。

linux下安装nginx

下载nginx

下载地址

下载可能需要的依赖,zlib, pcre

在执行./configure 时,如果出现以下情况:

如果出现如下,则需要下载cpre,下载地址

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

如果出现如下,则需要下载zlib, 下载地址

./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.

两个依赖如果下载不了,请自行百度查找解决方案了。

配置

下载完成后,执行:

$ ./configure --prefix=$HOME/nginx --with-pcre=$HOME/dev/pcre-8.42 --with-zlib=$HOME/dev/zlib-1.2.11

....

Configuration summary
  + using PCRE library: /home/myzhao/dev/pcre-8.42
  + OpenSSL library is not used
  + using zlib library: /home/myzhao/dev/zlib-1.2.11

  nginx path prefix: "/home/myzhao/nginx"
  nginx binary file: "/home/myzhao/nginx/sbin/nginx"
  nginx modules path: "/home/myzhao/nginx/modules"
  nginx configuration prefix: "/home/myzhao/nginx/conf"
  nginx configuration file: "/home/myzhao/nginx/conf/nginx.conf"
  nginx pid file: "/home/myzhao/nginx/logs/nginx.pid"
  nginx error log file: "/home/myzhao/nginx/logs/error.log"
  nginx http access log file: "/home/myzhao/nginx/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 && make install

安装目录即为 --prefix 指定的目录。只要不出现错误,即安装成功了。

运行

进入安装目录,再进入sbin目录,执行:

$ ./nginx

启动默认的端口为80,如果用户没有使用80端口的权限,请修改配置文件nginx.conf中的端口或使用root用户启动即可,前提是80端口没有被占用。

检查

打开浏览器,输入: http://127.0.0.1,显示如下,则启动成功了。

Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

其他相关命令请查看帮助信息./nginx -h,在此就不一一解释内容了。

nginx version: nginx/1.15.3
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /home/myzhao/nginx/)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file

其他

修改浏览器Response Headers中Server信息

修改浏览器server信息,目的是隐藏服务器所使用的web服务器的信息,尽量保证安全,以防被hacker根据web服务器版本,找到相关漏洞进行攻击行为。默认安装之后浏览器Response Headers中Server信息显示为:

Server: nginx/1.15.3

打开nginx源码目录,进入core目录,找到nginx.h文件,修改如下内容:

#define nginx_version      1015003
#define NGINX_VERSION      "1.15.3"
#define NGINX_VER          "nginx/" NGINX_VERSION

其中 NGINX_VERSION 可随意修改或留空字符串,NGINX_VER则可以修改为,自己想要的字符串,如:“zhangsan”,"JiuShiNB",

#define nginx_version      1015003
#define NGINX_VERSION      "8888"
#define NGINX_VER          "JiuShiNB/" NGINX_VERSION

修改完成之后,然后再重复配置、编译、安装等操作即可。如,修改后浏览器Response Headers中Server信息显示如下:

Server: JiuShiNB/8888

当然,不修改源码情况下,也是可以的,如修改配置文件中的相关配置,但是修改配置文件的方式只能隐藏版本号信息,并不能直接隐藏nginx信息。

简单的负载均衡配置

编辑 nginx.conf 配置文件,增加upstream如下

upstream app_server {
    server 127.0.0.1:1234 weight=1;
    server 127.0.0.1:1235 weight=1;
    server 127.0.0.1:1236 weight=1;
}

配置location如下

location /app {
    proxy_pass   http://app_server;
}

此时在浏览器中访问http://127.0.0.1/app将根据weight配置,均衡地被转发到127.0.0.1:1234,127.0.0.1:1235,127.0.0.1:1236上去了。weight: 默认为1.weight越大,负载的权重就越大,除了weight,还可以是以下配置:

  1. down: 表示单前的server暂时不参与负载
  2. max_fails:允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
  3. fail_timeout:max_fails 次失败后,暂停的时间。
  4. backup:其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
  5. upstream详情配置 参考
  6. location详细配置 参考

转载于:https://my.oschina.net/who7708/blog/2051911

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值