nginx代理基本配置

nginx 特色:把N个服务器进行代理,用户只需要访问nginx代理服务器就能访问(被代理的服务器),负载均衡

ubuntu安装:

//nginx需要的插件
apt-get install gcc
apt-get install g++
//nginx本体安装
apt-get install nginx
//关闭防火墙并开放80端口
ufw disable
ufw allow 80
//启动nginx服务
service nginx start

centos安装:

下载安装包
安装顺序是:
1.openssl
2.pcre
3.zlib
4.nginx

安装指令:
tar -zxvf 安装包的全名
cd 刚解压编译后的文件夹
./configure                     ←若显示没有此文件可以尝试首字母大写或小写,同时提高该文件的权限
./config(openssl专用)
make
make install
------------------------------------------------------------------------------------这就是安装的全部过程-------------------------------------------------------
实战demo:
开启服务:service nginx start 

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
	worker_connections 768;
	# multi_accept on;
}

http {

	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	keepalive_timeout 65;
	types_hash_max_size 2048;


	include /etc/nginx/mime.types;
	default_type application/octet-stream;



	ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
	ssl_prefer_server_ciphers on;

	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;

	gzip on;
	gzip_disable "msie6";
    
    #开始配置被代理的服务器
	upstream aserver{
    #设置多个服务器session共享
	ip_hash;
    #被代理的服务器 使用权重
	server 192.168.3.29:8080 weight=4;
	server 192.168.3.17:8080 weight=4;
	}
    #开始设置server,监听80端口
   	server {
        listen 80;
	    server_name aserver;
        #用户访问nginx的地址,下面是访问根目录首页
        location / {
            #proxy_pass 对应http://加上上面配置的upstream服务器的名字(被代理服务器配置)
            proxy_pass http://aserver;
        	} 
    	}
    #
	include /etc/nginx/conf.d/*.conf;
	#这个是默认配置,每个版本的nginx都不一样,下面注释掉的是nginx原先首页的配置
	#include /etc/nginx/sites-enabled/*;
}

修改完之后执行命令,更新nginx
./nginx -s reload
最后直接访问配置nginx的ip即可(默认80端口,上面listen也是80端口)
http://配置nginx服务器的ip/
                   

猜你喜欢

转载自blog.csdn.net/chijiajing/article/details/83745007
今日推荐