nginx反向代理服务器以及负载均衡,从安装到配置

nginx的具体作用不用细说,很强大,做负载均衡、反向代理服务器解决前端跨域问题等等。下面是nginx的安装过程

首先nginx主要的依赖:

1 pcre、 pcre-devel 
2 zlib zlib-devel 
3 openssl openssl-devel 

因此安装nginx需要安装以下依赖(本教程只介绍centos在线安装,离线安装见另一篇文章)

1.安装依赖

1 yum install gcc
2 yum install pcre-devel
3 yum install zlib zlib-devel
4 yum install openssl openssl-devel

2.下载解压nginx

2 cd /usr/local
3 mkdir nginx
4 cd nginx
5 //下载tar包
6 wget http://nginx.org/download/nginx-1.13.7.tar.gz
7 tar -xvf nginx-1.13.7.tar.gz             (解压文件)

3.安装nginx

1 //进入nginx目录
2 cd /usr/local/nginx-1.13.7
3 //执行命令
4 ./configure
5 //执行make命令
6 make
7 //执行make install命令
8 make install

4.配置nginx

cd到nginx配置文件,配置相应的信息数据,主要是配置好upstream 以及server即可,现在贴出我本机的配置

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

 5.测试nginx是否配置正确

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

如果出现以下所示,则配置没有问题

6.nginx的启停

启动
[root@localhost ~]# /usr/local/nginx/sbin/nginx
停止/重启
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop(quit、reload)
命令帮助
[root@localhost ~]# /usr/local/nginx/sbin/nginx -h
验证配置文件
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

其他介绍

1.VIM语法

默认vim打开后是不能录入的,需要按键才能操作,具体如下:
开启编辑:按“i”或者“Insert”键
退出编辑:“Esc”键
退出vim:“:q”
保存vim:“:w”
保存退出vim:“:wq”
不保存退出vim:“:q!”

2.nginx配置语法介绍

  1 #user  nobody;  
  2 
  3 #开启进程数 <=CPU数   
  4 worker_processes  1;  
  5 
  6 #错误日志保存位置  
  7 #error_log  logs/error.log;  
  8 #error_log  logs/error.log  notice;  
  9 #error_log  logs/error.log  info;  
 10 
 11 #进程号保存文件  
 12 #pid        logs/nginx.pid;  
 13 
 14 #每个进程最大连接数(最大连接=连接数x进程数)每个worker允许同时产生多少个链接,默认1024  
 15 events {  
 16     worker_connections  1024;  
 17 }  
 18 
 19 
 20 http {  
 21     #文件扩展名与文件类型映射表  
 22     include       mime.types;  
 23     #默认文件类型  
 24     default_type  application/octet-stream;  
 25 
 26     #日志文件输出格式 这个位置相于全局设置  
 27     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
 28     #                  '$status $body_bytes_sent "$http_referer" '  
 29     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
 30 
 31     #请求日志保存位置  
 32     #access_log  logs/access.log  main;  
 33 
 34     #打开发送文件  
 35     sendfile        on;  
 36     #tcp_nopush     on;  
 37 
 38     #keepalive_timeout  0;  
 39     #连接超时时间  
 40     keepalive_timeout  65;  
 41 
 42     #打开gzip压缩  
 43     #gzip  on;  
 44 
 45     server {  
 46         #监听端口,默认是80端口  
 47         listen       80;  
 48         #监听域名  
 49         server_name  localhost;  
 50 
 51         #charset koi8-r;  
 52 
 53         #nginx访问日志放在logs/host.access.log下,并且使用main格式(还可以自定义格式)  
 54         #access_log  logs/host.access.log  main;  
 55 
 56         #如果没有location更明确的匹配访问路径的话,访问请求都会被该location处理。  
 57         location / {  
 58             #root指定nginx的根目录为/usr/local/nginx/html  
 59             root   html;  
 60             #默认访问文件,欢迎页先去html目录下找index.html,如果找不到再去找index.htm  
 61             index  index.html index.htm;  
 62         }  
 63 
 64         #error_page  404              /404.html;  
 65         # redirect server error pages to the static page /50x.html  
 66         #  
 67 
 68         #错误页面及其返回地址,错误码为500、502、503、504都会返回50.html错误页面。  
 69         error_page   500 502 503 504  /50x.html;  
 70         #location后面是"="的话,说明是精确匹配  
 71         location = /50x.html {  
 72             root   html;  
 73         }  
 74 
 75         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
 76         #  
 77         #location ~ \.php$ {  
 78         #    proxy_pass   http://127.0.0.1;  
 79         #}  
 80 
 81         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
 82         #  
 83         #location ~ \.php$ {  
 84         #    root           html;  
 85         #    fastcgi_pass   127.0.0.1:9000;  
 86         #    fastcgi_index  index.php;  
 87         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
 88         #    include        fastcgi_params;  
 89         #}  
 90 
 91         # deny access to .htaccess files, if Apache's document root  
 92         # concurs with nginx's one  
 93         #  
 94         #location ~ /\.ht {  
 95         #    deny  all;  
 96         #}  
 97     }  
 98 
 99 
100     # another virtual host using mix of IP-, name-, and port-based configuration  
101     #  
102     #server {  
103     #    listen       8000;  
104     #    listen       somename:8080;  
105     #    server_name  somename  alias  another.alias;  
106 
107     #    location / {  
108     #        root   html;  
109     #        index  index.html index.htm;  
110     #    }  
111     #}  
112 
113 
114     # HTTPS server  
115     #  
116     #server {  
117     #    listen       443 ssl;  
118     #    server_name  localhost;  
119 
120     #    ssl_certificate      cert.pem;  
121     #    ssl_certificate_key  cert.key;  
122 
123     #    ssl_session_cache    shared:SSL:1m;  
124     #    ssl_session_timeout  5m;  
125 
126     #    ssl_ciphers  HIGH:!aNULL:!MD5;  
127     #    ssl_prefer_server_ciphers  on;  
128 
129     #    location / {  
130     #        root   html;  
131     #        index  index.html index.htm;  
132     #    }  
133     #}  
134 
135 }  
136 
137 ---------------------
138 
139 本文来自 PengTDY 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/yougoule/article/details/78186138?utm_source=copy 

猜你喜欢

转载自www.cnblogs.com/lidedong/p/9690832.html