nginx学习--安装与搭建可用的静态资源Web服务器

1. nginx 下载
下载地址,点击这里
2. nginx 安装
首先在home下新建一个文件夹叫nginx;
然后在进入下载问nginx安装包解压路径;
命令行输入 ./configure --prefix=/home/xxx(你的用户名)/nginx;
然后接着输入 make
最后输入 make install
3. nginx 启动与关闭
在终端输入./nginx,启动nginx服务;
打开浏览器,输入localhost:80,如果出现欢迎界面,说明nginx安装成功
如果要关闭nginx,在终端输入./nginx -s stop,(其实还有其他方法,目前先不管);
如果更新了nginx的相关设置,不需要重启nginx,只要在终端输入nginx -s reload就可以了;
ps:conf/nginx.conf 文件进行nginx相关设置
4. nginx 搭建可用的静态资源Web服务器
首先在当前路径下创建一个用来存放文件的文件夹,将几个英文名的文件放到此文件夹下(汉语打不开,这个问题后续再解决), 在 http --> server 部分添加如下代码,然后在终端输入./nginx -s reload重新加载配置,然后打开浏览器输入 localhost:80 就可以看到文件夹目录了:

location / {
            alias test_doc/;     # 我自己新建的文件夹叫 test_doc
            autoindex on;        # 打开目录浏览功能,如果不打开这个功能,
                                 # 必须输入文件名才可以打开文件,
                                 # 比如我有一个叫1.pdf的文件,那我要输入
                                 # localhost:80/1.pdf 才可以正确显示,
                                 # 打开之后我输入localhost:80,就会显示整个文件夹
            set $limit_rate 1m;  # 指定每秒该连接能下载的bytes,主要用来限制个别请求的带宽,
                                 # 防止大文件占用太高的带宽,导致其他连接阻塞
        }

5. nginx 负载均衡
我们可以通过修改nginx的配置文件nginx.conf,来实现当访问nginx服务器的时候跳转到代理服务器,可以在 http --> server 部分加入如下代码,注意第四部分添加的代码要注释掉,然后在终端输入./nginx -s reload重新加载配置,然后打开浏览器输入 localhost:80 就可以看到跳转到代理服务器了:

location / {
            proxy_pass https://www.baidu.com;
        }

6. nginx 日志切分
在 nginx.conf 的 http 部分找到如下代码,取消注释

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

然后可以在 nginx 中创建一个 log 文件夹,接下来在 http --> server 部分添加一行代码,然后在终端输入./nginx -s reload重新加载配置,点击几个文件,然后去刚才创建的 log 文件夹下就会看到一个日志文件,打开之后就是刚才单击事件的记录:

access_log test_log/test_log.log main;   # 我创建的log文件夹是test_log

7. 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;

        access_log test_log/test_log.log main; # 我创建的log文件夹是test_log

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        location / {
            alias test_doc/;     # 我自己新建的文件夹叫 test_doc
            autoindex on;        # 打开目录浏览功能,如果不打开这个功能,
                                 # 必须输入文件名才可以打开文件,
                                 # 比如我有一个叫1.pdf的文件,那我要输入
                                 # localhost:80/1.pdf 才可以正确显示,
                                 # 打开之后我输入localhost:80,就会显示整个文件夹
            set $limit_rate 1m;  # 指定每秒该连接能下载的bytes,主要用来限制个别请求的带宽,
                                 # 防止大文件占用太高的带宽,导致其他连接阻塞
            # proxy_pass https://www.baidu.com;
        }

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

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章