Nginx

Nginx

nginx文档

下载

download

在这里插入图片描述

# 解压
tar -zxvf nginx-1.20.2.tar.gz
# 剪切
mv nginx-1.20.2 /usr/local/nginx
# 进入nginx目录
cd /usr/local/nginx
# 没有gcc环境,安装
yum install -y gcc
# 
./configure --prefix=/usr/local/nginx
# 安装perl
yum install -y pcre pcre-devel
# 安装
yum install -y zlib zlib-devel

看到这个就是success
在这里插入图片描述

# 编译
make & make install
# 启动
cd sbin
./nginx ../conf/nginx.conf

查看

ps -ef|grep nginx

在这里插入图片描述

访问:success
在这里插入图片描述

命令行参数

# 启动
/usr/local/nginx/sbin/nginx -c nginx.conf
# 停止
/usr/local/nginx/sbin/nginx -s stop
# 重新加载
/usr/local/nginx/sbin/nginx -s reload
# 版本查看
/usr/local/nginx/sbin/nginx -v
# 检测文件错误
/usr/local/nginx/sbin/nginx -t -c 文件名
# 端口查看
netstat -anp | more

配置文件

在这里插入图片描述

nginx.conf

# 全局快
#user  nobody;

# 工作进程数目 通常等于CPU数量或2*CPU数量
worker_processes  1;

# 错误日志存放路径
#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;
}

# http块
http {
	# 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块
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
		
		# location块
        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;
    #    }
    #}
}

正向代理

代理的对象是客户端(浏览器)

反向代理

代理的对象是服务端,访问不同的服务。

修改配置文件nginx.conf,代理到8081
此处虚拟机内需要自行开启tomcat 8081的服务,否则无效。

server {
        listen       80;
        # 代理服务器ip
        server_name  192.168.10.129;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;


        location / {
            root   html;
            # 代理到服务器端口为8081的应用
            proxy_pass http://127.0.0.1:8081;
            index  index.html index.htm;
        }
		...
}
# 重新reload
/usr/local/nginx/sbin/nginx -s reload

host文件配置,可选

192.168.10.129 www.lcq.com

访问:http://www.lcq.com或http://192.168.10.129
在这里插入图片描述

location

Location

  • 精准匹配:=
  • 普通匹配:存在^~直接返回,否则匹配多个取最长
  • 正则匹配:~~*!~!~*,匹配成功直接返回

负载均衡

访问相同功能的服务(集群)

nginx.conf

...
upstream  servername{
    #轮询
	#server ip:port;
	#server ip:port;
	#权重
	server ip:port weight=1;
	server ip:port weight=2;
}
...
server {
	...
	location / {
		proxy_pass http://servername;
	}
}
...

servername不可以用下划线命名

动静分离

动态资源放置tomcat服务器,静态资源存放在nginx
nginx.conf

server {
       ...

        location /edu/ {
         	#root   html;
	    	#proxy_pass http://127.0.0.1:8081;
	    	proxy_pass http://myserver;#动态资源
            index  index.html index.htm;
        }
        
		location /edu/img/ {
	    	root /usr/local/nginx/html;#静态资源
		} 	
		...
}

工作机制

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值