Liunx下Nginx安装配置

Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。

1. 下载&安装 

首先我们使用wget下载nginx压缩包并且通过tar命令解压出来

wget http://nginx.org/download/nginx-1.17.4.tar.gz
tar -xzvf nginx-1.17.4.tar.gz

进入nginx目录,执行目录下的configure

  1. 如果需要指定nginx安装目录则是 configure --prefix=[Nginx install path]
  2. 如果需要指定nginx配置文件目录则是 configure --conf-path=[Nginx conf path]
  3. 如果需要配置Https则需要安装SSL模块 configure --with-http_ssl_module
  4. 监控nginx当前状态可以安装此模块 configure --with-http_stub_status_module
  5. Nginx还有其他选项,如果有需要读者可以去Nginx文档
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

如果提示错误 

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

是因为缺少pcre依赖包,可以使用yum(Fedora、RedHat、CentOS)进行安装。

yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel

然后我们进行编译安装

make && make install

我们可以通过配置环境变量在全局环境下执行nginx。打开etc目录下的profile文件

vim /etc/profile

在最后添加 export NGINX_PATH=/usr/local/nginx/sbin/ 和 export PATH=$PATH:$NGINX_PATH,保存退出后执行source命令使其生效

source /etc/profile

此时我们可以在任何地方执行nginx命令了。

2. 配置

打开nginx/conf目录下的nginx.conf文件

vim /usr/local/nginx/conf/nginx.conf

在http节点下添加

server {
    listen 80; // 监听80端口
    server_name testserver; // 转发到指定地址:端口
    location / {
        root html;
        index index.html index.html;
        // 在nginx中配置proxy_pass代理转发时,如果在proxy_pass后面的url加/,表示绝对根路径;如果没有/,表示相对路径,把匹配的路径部分加入。
        proxy_pass http://www.testserver.com/;  // 代理到指定地址:端口
        // 获取用户真实Ip地址
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;						
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    }
}
// 定义一组服务器
upstream testserver {
    server ip:port; // 代理的地址 + 端口
    # upstream分配方式
    1. 轮询,即每个请求按照时间顺序轮流分配到不同的后端服务器,如果某个后端服务器down掉后,能自动剔除。
    2.weight 即可以指定轮询比率,weight和访问几率成正比,主要应用于后端服务器异质的场景下。server ip:port weight=1; server ip:port weight=2;
    3.ip_hash 每个请求按照访问ip(即Nginx的前置服务器或者客户端IP)的hash结果分配,这样每个访客会固定访问一个后端服务器,可以解决session一致问题。 在顶部加上ip_hash即可
    4. fair fair顾名思义,公平地按照后端服务器的响应时间(rt)来分配请求,响应时间短即rt小的后端服务器优先分配请求。
    5. url_hash 与ip_hash类似,但是按照访问url的hash结果来分配请求,使得每个url定向到同一个后端服务器,主要应用于后端服务器为缓存时的场景下。
}

如果需要配置Https则添加

# 将证书上传到服务器上
# 监听443端口
server {
	#监听443端口。443为知名端口号,主要用于HTTPS协议
	listen       443 ssl;

	#定义使用www.xx.com访问
	server_name  www.aabbccdd.com;

	#ssl证书文件位置(常见证书文件格式为:crt/pem)
	ssl_certificate      cert.pem;
	#ssl证书key位置
	ssl_certificate_key  cert.key;

	#ssl配置参数(选择性配置)
	ssl_session_cache    shared:SSL:1m;
	ssl_session_timeout  5m;
	#数字签名,此处使用MD5
	ssl_ciphers  HIGH:!aNULL:!MD5;
	ssl_prefer_server_ciphers  on;

	location / {
	  root   /root;
	  index  index.html index.htm;
	}
	
	#编码格式
	charset utf-8;
	
	#代理配置参数
	proxy_connect_timeout 180;
	proxy_send_timeout 180;
	proxy_read_timeout 180;
	proxy_set_header Host $host;
	proxy_set_header X-Forwarder-For $remote_addr;

	#反向代理的路径(和upstream绑定),location 后面设置映射的路径
	location / {
		proxy_pass http://zp_server1;
	} 

	#静态文件,nginx自己处理
	location ~ ^/(images|javascript|js|css|flash|media|static)/ {
		root C:/XMCARES_X/WorkSpace/nginx/src/main/webapp/views;
		#过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
		expires 30d;
	}

	#设定查看Nginx状态的地址
	location /NginxStatus {
		stub_status           on;
		access_log            on;
		auth_basic            "NginxStatus";
		auth_basic_user_file  conf/htpasswd;
	}

	#禁止访问 .htxxx 文件
	location ~ //.ht {
		deny all;
	}
	
	#错误处理页面(可选择性配置)
	#error_page   404              /404.html;
	#error_page   500 502 503 504  /50x.html;
	#location = /50x.html {
	#    root   html;
	#}
}

3. 启动&停止

启动Nginx(指定配置文件,也可以直接启动,默认配置文件是nginx/conf/nginx.conf)

/usr/local/nginx/sbin/nginx -c [Nginx conf path]

停止Nginx

/usr/local/nginx/sbin/nginx -s stop

重载(更改配置文件等)

/usr/local/nginx/sbin/nginx -s reload

测试Nginx配置文件是否正常

/usr/local/nginx/sbin/nginx -t

如果正常则会提示

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

如果需要添加开机自动启动则在/etc/rc.local文件下添加nginx路径[/usr/local/nginx/sbin/nginx]

cat >> /etc/rc.local << EOF
/usr/local/nginx/sbin/nginx
EOF

 

你可能感兴趣的:(Liunx,Nginx,反向代理)