Nginx第1篇-安装和简单配置

Nginx可以做什么

  1. 可以做静态HTTP服务器
  2. 做负载均衡
  3. 做正向代理
  4. 做反向代理

正向代理和反向代理

正向代理:

是一个位于客户端和目标服务器之间的服务器(代理服务器),为了从目标服务器取得内容,客户端向代理服务器发送一个请求并指定目标,然后代理服务器向目标服务器转交请求并将获得的内容返回给客户端。常用的比如访问国外网站。所以,正向代理,其实是"代理服务器"代理了"客户端",去和"目标服务器"进行交互。

反向代理:

是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。
所以,反向代理,其实是"代理服务器"代理了"目标服务器",去和"客户端"进行交互。

系统版本

本次安装基于系统版本:CentOS7.6,使用下面指令查看linux系统内核版本

[root@VM-0-9-centos ~]#  uname -a
Linux VM-0-9-centos 3.10.0-1160.108.1.el7.x86_64 #1 SMP Thu Jan 25 16:17:31 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

Nginx下载

nginx下载地址:https://nginx.org/en/download.html
本次选择:http://nginx.org/download/nginx-1.9.2.tar.gz

安装过程

参考文档:https://blog.csdn.net/qq_39420519/article/details/126322909
linux可以将软件安装在/opt目录下,也可以自己选择。

cd /opt
# step1: 下载软件包
wget http://nginx.org/download/nginx-1.9.2.tar.gz
# step2: 解压
tar -xzvf nginx-1.9.2.tar.gz
# step3: 编译
cd nginx-1.9.2/
make && make install 
# step4 设置软链:
ln -s /usr/local/nginx/sbin/nginx /usr/bin/
# step5 启动, 直接执行nginx就可以了
nginx

其他常用的命令

# 其他的一些常用命令
# 检查配置
nginx -v 查看版本
nginx -t(检查nginx.conf配置是否正确)
nginx -s reload(重新载入配置文件,通常配合-t使用,在修改了nginx.conf且检查无误之后)
nginx -s stop(停止 Nginx)
nginx -s reopen(重启 Nginx)

启动之后,在浏览器输入ip地址就能看到被代理到默认界面了。
在这里插入图片描述

配置

我们看下配置,nginx是如何代理的

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

可以看到上面监听了80端口,然后代理一些静态html资源。

下面我们增加个配置,将我们的请求代理到www.baidu.com:

    server {
        listen       81;
        # server_name  somename  alias  another.alias; # 也可以写ip

        location / {
            proxy_pass   http://www.baidu.cm;
        }
    }
  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值