设为首页 加入收藏

TOP

Nginx学习笔记--001-Nginx快速搭建
2017-10-13 10:40:52 】 浏览:8592
Tags:Nginx 学习 笔记 --001-Nginx 快速 搭建

Nginx ("engine x") 是一个高性能的HTTP反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由Igor Sysoev为俄罗斯访问量第二的Rambler.ru站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。关于Nginx的优点,我在这里就不讨论了,网上的资料多的是。

如何使用Nginx快速搭建一个高性能的网站?

1.首先去Nginx官网下载一个最新版本的Nginx,下载地址:http://nginx.org/en/download.html。我这里下载的版本是:nginx/Windows-1.11.6。下载完成之后,得到一个.zip的压缩包,把压缩包解压到D盘根目录。如图1-1

 

图1-1

2.修改配置文件。打开“conf”文件夹下的“nginx.conf”文件。其中修改的主要部分见图1-2。

 

图1-2

下面是我修改后的完整配置文件信息:

#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;#开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65; #长连接超时时间,单位是秒

    #gzip  on; #启用Gizp压缩

	#服务器的集群
    upstream  rj.nginx.com {  #服务器集群名字	
		server    127.0.0.1:8001  weight=1;#服务器配置   weight是权重的意思,权重越大,分配的概率越大。
		server    127.0.0.1:8002  weight=2;
	}
	
	#当前的Nginx的配置
    server {
        listen       80; #监听80端口,可以改成其他端口
        server_name  localhost; # 当前服务的域名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://rj.nginx.com;
            proxy_redirect default;
        }
		location ~ \.(jpg|png|jpeg|bmp|gif|swf|css)$
        {
         
            expires 30d;
             root /nginx-1.4.7;#root:
            break;
        }

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

}
View Code

3.打开命令行,定位到Nginx当前目录,使用“start nginx”命令启动nginx。如图1-3

图1-3

 4.打开浏览器,输入:http://localhost/。可以发现,浏览的网站,会在两个站点见不停的切换,并且站点2的使用几率会稍高一些,因为站点2的权重设置的比站点1的大。如图1-4

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇高性能 TCP/UDP/HTTP 通信框架 HP.. 下一篇gRPC源码分析1-SSL/TLS

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目