Nginx快速学习

没有是加一层解决不了的!

Nginx是一个高性能的HTTP和反向代理web服务器,内存小,并发能力强

Nginx作用

HTTP代理,反向代理,作为web服务器最常用的功能之一,尤其是方向代理

正向代理:VPN,代理客户端

反向代理:Nginx,代理服务端

负载均衡

  • 轮询
  • 加权轮询
  • iphash:对客户端请求的ip进行hash操作,根据hash将同一个客户端ip的请求分发给同一台服务器进行处理,可以解决session不共享的问题(但是可能性能上不是很好,如果服务器挂了,信息会丢失,所以最好用redis做session共享)

动静分离:静态资源在本地,动态在web端

windows安装

下载链接

选择stable版本,下载后直接解压安装

image-20220106214111434

主要文件:

image-20220106214255327

只要在这里设置了端口启动Nginx之后访问这个端口就会被拦截

image-20220106214451203

启动

image-20220106214640553

此时访问就会显示welcome to nginx!

Linux安装

下载链接

image-20220106214943634

1. 确保本机没有nginx

[root@Jeseany ~]# whereis

Usage:
 whereis [options] file

Options:
 -b         search only for binaries
 -B <dirs>  define binaries lookup path
 -m         search only for manuals
 -M <dirs>  define man lookup path
 -s         search only for sources
 -S <dirs>  define sources lookup path
 -f         terminate <dirs> argument list
 -u         search for unusual entries
 -l         output effective lookup paths

For more details see whereis(1).
[root@Jeseany ~]# whereis nginx
nginx:[root@Jeseany ~]# nginx
-bash: nginx: command not found

2. 上传nginx压缩包直接解压

[root@Jeseany ~]# ll
total 1068
-rw-r--r-- 1 root root   25977 May 15  2021 install.sh
-rw-r--r-- 1 root root 1062124 Jan  6 21:53 nginx-1.20.2.tar.gz
[root@Jeseany ~]# tar -zxvf nginx-1.20.2.tar.gz 

3.进入目录可以查看文件

[root@Jeseany ~]# cd nginx-1.20.2
[root@Jeseany nginx-1.20.2]# ll
total 812
drwxr-xr-x 6 mysql mysql   4096 Jan  6 21:55 auto
-rw-r--r-- 1 mysql mysql 312251 Nov 16 22:44 CHANGES
-rw-r--r-- 1 mysql mysql 476577 Nov 16 22:44 CHANGES.ru
drwxr-xr-x 2 mysql mysql   4096 Jan  6 21:55 conf
-rwxr-xr-x 1 mysql mysql   2590 Nov 16 22:44 configure   #这个是绿色的,执行配置文件
drwxr-xr-x 4 mysql mysql   4096 Jan  6 21:55 contrib
drwxr-xr-x 2 mysql mysql   4096 Jan  6 21:55 html
-rw-r--r-- 1 mysql mysql   1397 Nov 16 22:44 LICENSE
drwxr-xr-x 2 mysql mysql   4096 Jan  6 21:55 man
-rw-r--r-- 1 mysql mysql     49 Nov 16 22:44 README
drwxr-xr-x 9 mysql mysql   4096 Jan  6 21:55 src

4.执行配置文件configure然后再make

[root@Jeseany nginx-1.20.2]# ./configure
[root@Jeseany nginx-1.20.2]# make
[root@Jeseany nginx-1.20.2]# make install

5. 安装好查看Nginx的位置

niginx:[root@Jeseany nginx-1.20.2]# whereis nginx
nginx: /usr/local/nginx

6. 进入nginx的文件,并启动

[root@Jeseany nginx-1.20.2]# cd /usr/local/nginx
[root@Jeseany nginx]# ll
total 16
drwxr-xr-x 2 root root 4096 Jan  6 21:59 conf
drwxr-xr-x 2 root root 4096 Jan  6 21:59 html
drwxr-xr-x 2 root root 4096 Jan  6 21:59 logs
drwxr-xr-x 2 root root 4096 Jan  6 21:59 sbin
[root@Jeseany nginx]# cd sbin/
[root@Jeseany sbin]# ll
total 3796
-rwxr-xr-x 1 root root 3883616 Jan  6 21:59 nginx
[root@Jeseany sbin]# ./nginx       #这里没有报错就是ok了,如果错了查看端口是否被占用或者改端口,还有防火墙

错误情况

image-20220106220531009

解决

解决端口占用问题

  1. 防火墙是否开启
  2. 安全组是否设置
  3. 进入Nginx的配置文件里,发现监听了80,实际上80被占用
[root@Jeseany sbin]# cd ..
[root@Jeseany nginx]# ll
total 36
drwx------ 2 nobody root 4096 Jan  6 22:02 client_body_temp
drwxr-xr-x 2 root   root 4096 Jan  6 21:59 conf
drwx------ 2 nobody root 4096 Jan  6 22:02 fastcgi_temp
drwxr-xr-x 2 root   root 4096 Jan  6 21:59 html
drwxr-xr-x 2 root   root 4096 Jan  6 22:02 logs
drwx------ 2 nobody root 4096 Jan  6 22:02 proxy_temp
drwxr-xr-x 2 root   root 4096 Jan  6 21:59 sbin
drwx------ 2 nobody root 4096 Jan  6 22:02 scgi_temp
drwx------ 2 nobody root 4096 Jan  6 22:02 uwsgi_temp
[root@Jeseany nginx]# cd conf/
[root@Jeseany conf]# ll
total 68
-rw-r--r-- 1 root root 1077 Jan  6 21:59 fastcgi.conf
-rw-r--r-- 1 root root 1077 Jan  6 21:59 fastcgi.conf.default
-rw-r--r-- 1 root root 1007 Jan  6 21:59 fastcgi_params
-rw-r--r-- 1 root root 1007 Jan  6 21:59 fastcgi_params.default
-rw-r--r-- 1 root root 2837 Jan  6 21:59 koi-utf
-rw-r--r-- 1 root root 2223 Jan  6 21:59 koi-win
-rw-r--r-- 1 root root 5231 Jan  6 21:59 mime.types
-rw-r--r-- 1 root root 5231 Jan  6 21:59 mime.types.default
-rw-r--r-- 1 root root 2656 Jan  6 21:59 nginx.conf
-rw-r--r-- 1 root root 2656 Jan  6 21:59 nginx.conf.default
-rw-r--r-- 1 root root  636 Jan  6 21:59 scgi_params
-rw-r--r-- 1 root root  636 Jan  6 21:59 scgi_params.default
-rw-r--r-- 1 root root  664 Jan  6 21:59 uwsgi_params
-rw-r--r-- 1 root root  664 Jan  6 21:59 uwsgi_params.default
-rw-r--r-- 1 root root 3610 Jan  6 21:59 win-utf
[root@Jeseany conf]# cat nginx.conf

#user  nobody;
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 {
    worker_connections  1024;
}


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;
    #    }
    #}

}

改端口

[root@Jeseany conf]# vim nginx.conf

【I】进入编辑insert

【ESC】退出编辑

【:】等待指令

【wq】写入退出

image-20220106222701218

此时访问ip+端口既是welcome to nginx!

image-20220106223533186

常用命令

[root@Jeseany sbin]# ./nginx -s stop     #停止(强制)
[root@Jeseany sbin]# ./nginx -s quit     #安全退出
#改配置文件需要重新加载配置文件,此时需要先启动
[root@Jeseany sbin]# ./nginx
[root@Jeseany sbin]# ./nginx -s reload
#查看Nginx进程
[root@Jeseany sbin]# ps aux|grep nginx
root      7213  0.0  0.0 112812   976 pts/0    R+   22:36   0:00 grep --color=auto nginx

配置文件

就是说如果有两个启动了的项目,端口不一样,但是用户不可能去访问两个端口地址,这个时候需要一个Nginx进行反向代理,用户只需要访问Nginx的端口地址就行了

不同服务器也是一样的

image-20220106224500600 image-20220106224537850 image-20220106225229346

可以百度Nginx的配置

快速入门文档

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值