Nginx 入门(windows)

Nginx("engine x"):高性能的Web和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。

反向代理(Reverse Proxy):以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。 

1、安装

Nginx下载地址:官网地址:http://nginx.org/en/download.html

下载完成直接解压,我是解压到D盘,D:\nginx-1.15.0。

在D:\nginx-1.15.0目录下,打开命令窗口,输入 start nginx,启动nginx。


然后确定启动成功,直接访问http://localhost可以看到:


2、配置说明

在nginx里面有一个很重要的配置文件nginx.conf。在nginx目录下。有一个conf文件夹,其他的先不管,我们先打开nginx.conf。

#运行用户,默认即是nginx,可不设置
#user  nobody;
#nginx进程,一般设置为和cpu核数一样
worker_processes  1;

#错误日志存放目录
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#进程pid存放位置
#pid        logs/nginx.pid;

#工作模式及连接数上限
events {
    worker_connections  1024; #单个后台worker process进程的最大并发链接数
}


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参数可以允许把httpresponse header和文件的开始放在一个文件里发布,积极的作用是减少网络报文段的数量
    #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;
    #    }
    #}

}

其中,最主要的是server,它相当于一个代理服务器,可以配置多个。

listen:表示当前代理服务器监听的端口,默认80,如果配置多个,端口需要不一样,否则不能确定转到哪里

server_name:表示监听到之后需要转到哪里,上面配置是直接转到本地,也就是nginx文件夹内。

location:表示匹配的路径,上面配置了 / 表示所有请求都被匹配到这里,正常使用会将文件类型进行区分过滤。

root:上面配置的html表示,当匹配这个请求的路径时,将会在nginx目录下的html文件夹内寻找相应的文件。

index:当没有指定主页时,默认会选择这个指定的文件,它可以有多个,并按顺序来加载。

3、简单使用

访问localhost时转到tomcat,需要修改server配置。

server_name localhost:8080;  
  
location / { 
    root   html;
    index  index.html index.htm; 
    proxy_pass  http://localhost:8080;
}  

tomcat在8080端口,根据实际情况修改。

proxy_pass,它表示代理路径,相当于转发。

修改配置文件,就必须重新启动,nginx -s reload 。nginx -t 查看配置文件是否有误。nginx -s stop 关闭nginx。

重新打开http://localhost


正常使用,我们需要按文件类型进行过滤,比如jsp直接给tomcat处理,因为nginx不是servlet容器,不能处理JSP文件,而HTML,JS,CSS这些不需要处理的,直接给nginx进行缓存。

这时最主要用的还是location这个元素,并且涉及到一部分正则。

并且需要去掉之前配的location /,避免全部请求被拦截了。

location ~ \.jsp$ {  
    proxy_pass http://localhost:8080;  
}  
          
location ~ \.(html|js|css|png|gif)$ {  
    root D:/apache-tomcat-7.0.55/webapps/ROOT;  
}  

上面的配置,表示jsp文件交由tomcat进行处理,而html,js等文件直接让nginx进行缓存。

但是现在我们打开http://localhost,会出现404 Not Found。因为我们没有相应的location匹配,所以当我们不指定jsp页面时,就会有404错误。

所以我们用http://localhost/index.jsp去访问,就又会出现tomcat的首页。但是当你点击Manager Application HOW-TO等链接时,又会出现404页面。主要是因为,这些html页面不在ROOT目录下,而在docs目录下,当我们匹配这些html时,我们是在ROOT下寻找,所以找不到这些页面。

一般情况下,我们都是会将所有的静态文件HTML,JS,CSS放到相同文件下,这样就不会出现这种情况了。类似tomcat这种,它是属于不同的项目,我也就没办法了。


很多时候,我们会在多台服务器上部署项目,防止有的服务器挂了,所以我们就可以使用upstream。

upstream local_tomcat {  
    server localhost:8080;
    server localhost:8081;
}  
  
server{  
        location / {  
           proxy_pass http://local_tomcat;  
        }  
}  

上面配置在server外添加了一个upstream,proxy_pass里面直接用http:// + upstream的名称来使用。

这种情况,nginx会随机将请求转发给这2台服务器,我们也可以添加weight来设置出现的概率,weight越大,请求的概率越大。

upstream local_tomcat {  
    server localhost:8080 weight=1;
    server localhost:8081 weight=2;
}  
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值