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