CentOS7,Nginx安装配置原理

参考文章
参考视频

安装包的存储位置
/usr/local/src/

安装

1.安装编译工具及库文件

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

2.安装 PCRE

PCRE 作用是让 Nginx 支持 Rewrite 功能。
1.下载

[root@bogon src]# cd /usr/local/src/
[root@bogon src]# wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz

2.解压

[root@bogon src]# tar zxvf pcre-8.35.tar.gz

3.进入安装包目录

[root@bogon src]# cd pcre-8.35

4.编译安装

[root@bogon pcre-8.35]# ./configure
[root@bogon pcre-8.35]# make && make install

5、查看pcre版本

[root@bogon pcre-8.35]# pcre-config --version

3.安装 Nginx

1、下载 Nginx,下载地址:https://nginx.org/en/download.html

[root@bogon src]# cd /usr/local/src/
[root@bogon src]# wget http://nginx.org/download/nginx-1.6.2.tar.gz

2、解压

tar zxvf nginx-1.6.2.tar.gz #解压安装包

3、进入安装包目录

cd nginx-1.6.2

4、编译安装

./configure #检查配置
make && make install #编译安装

5、查看nginx版本

/usr/local/nginx/sbin/nginx -v

6、执行sbin下的Nginx,运行Nginx

cd usr/local/nginx/sbin/
./nginx #运行

7、查看Nginx进程

ps -ef | grep nginx # 查看进程
root     18414     1  0 20:25 ?        00:00:00 nginx: master process ./nginx
nobody   18415 18414  0 20:25 ?        00:00:00 nginx: worker process
root     19486 16006  0 20:26 pts/0    00:00:00 grep --color=auto nginx

8、 确保防火墙已经开启了80端口,直接访问
在这里插入图片描述

Nginx常用命令

使用Nginx命令需要进入目录/usr/local/nginx/sbin

./nginx -v #查看版本
./nginx -s stop #关闭Nginx
./nginx #启动
./nginx -s reload #重新加载配置文件

配置文件

位置

/usr/local/nginx/conf目录下的nginx.conf

内容

#user  nobody;
#events之上的就是全局块
worker_processes  1; #Nginx处理并发的能力,值越大能力越强

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

#events 块
events {
    worker_connections  1024;#支持的最大连接数1024
}

#http块包含http全局块和server 块
http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;#监听的端口
        server_name  localhost;#监听的主机

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
#请求路径跳转
        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

在这里插入图片描述
在这里插入图片描述

实战

1.反向代理配置(server块)

访问localhost:80转到http://localhost:8080/test

 server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            proxy_pass http://localhost:8080/test; 
	    index  index.html index.htm;
        }

重新加载配置文件需要超级管理员权限

sudo nginx -s reload

2.配置不同路径跳转(server块)

访问localhost:80
路径带/cat/跳转http://localhost:8080/cat
路径带/dog/跳转http://localhost:8081/dog
访问http://localhost/c/cat/dog/会转到http://localhost:8080/c/cat/dog/
~ /cat/是正则

  server {
    	        listen       80;
    	        server_name  localhost;
    	        
    	        location ~ /cat/ {
    	            proxy_pass http://localhost:8080;
    	        }
    			location ~ /dog/{
    				proxy_pass http://localhost:8081;
    			}

注意proxy_pass属性只能是域名/IP+端口,不能是具体的访问路径否则错误信息如下

"proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block

在这里插入图片描述

3.负载均衡配置(http块,以及server块)

http块upstream
myserver自定义的服务名称
server负载均衡的服务

upstream myserver{
		server localhost:8080;
		server localhost:8082;
	}

server块

location / {
            proxy_pass http://myserver;
        }

负载均衡策略

1.默认轮询,(但我实际测试是随机的有可能版本不同),如果服务宕机,能够自动剔除
2.权重weight,默认为1,权重越高,分配越多

upstream myserver{
		server localhost:8080 weight=5;
		server localhost:8082 weight=10;
	}

3.ip_hash,根据客户端的ip固定分配,可以解决session共享问题(但是用户是4g网的时候,网络每次重启都是一个新的ip,这个模式挺废的,而且现在谁还用session啊,单机的玩意,推荐JWT)

upstream myserver{
		ip_hash;
		server localhost:8080;
		server localhost:8082;
	}

4.fair,响应时间短的优先分配

upstream myserver{
		server localhost:8080;
		server localhost:8082;
		fair;
	}

4.动静分离

1.静态页面(前端项目)单独放一台服务器
2.静态页面,单独放一个文件夹
软件初期,为了节约成本就用方式2,测试服务器也用方式2

静态文件路径

/Users/lbb/static/a.html

nginx配置
访问http://localhost/static/a.html会转到/Users/lbb/static/a.html
也就是root+location,屏蔽了目录的真实位置

	location /static/ {
            root /Users/lbb/;
        }

原理(看视频比较好)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值