搭建网站服务器 Nginx

2019-01-06  本文已影响10人  风之化身呀

0、安装准备

ln -s /usr/local/webserver/nginx/sbin/nginx /usr/local/bin
// 诸如 nginx 的配置文件,日志文件等
cd /usr/local/webserver/nginx
// 启动 nginx
nginx
// 修改配置文件后重启 nginx
nginx -s reload
// 终止 nginx
nginx -s stop
// 检查 配置文件 语法是否有误
nginx -t -c  **/**.conf

1、语法基础

location

语法: location [=||*|^~|@] /uri/ { … } 或 location @name { … }

一个请求过来后,Nginx匹配这个请求的流程如下:

location =/ { … }   // 精准匹配
location / { … }     // 模糊匹配,贪婪原则
location ~ /test/.+.jsp$ { … }  // 正则匹配
location ^~ / { … } //  ^~意思是关闭正则匹配,当搜索到这个普通匹配模式后,将不再继续搜索正则匹配模式

root 与 alias

当资源匹配上 location 时,
root: root + location配置的路径 = 资源在磁盘真实寻找路径
alias: alias = 资源在磁盘真实寻找路径

proxy-pass 中的 /

在nginx中配置proxy_pass代理转发时,如果在proxy_pass后面的url加/,表示绝对根路径;如果没有/,表示相对路径,把匹配的路径部分也给代理走。

假设下面四种情况分别用 http://192.168.1.1/proxy/test.html 进行访问。

第一种:
location /proxy/ {
proxy_pass http://127.0.0.1/;
}
代理到URL:http://127.0.0.1/test.html

第二种(相对于第一种,最后少一个 / )
location /proxy/ {
proxy_pass http://127.0.0.1;
}
代理到URL:http://127.0.0.1/proxy/test.html

第三种:
location /proxy/ {
proxy_pass http://127.0.0.1/aaa/;
}
代理到URL:http://127.0.0.1/aaa/test.html

第四种(相对于第三种,最后少一个 / )
location /proxy/ {
proxy_pass http://127.0.0.1/aaa;
}
代理到URL:http://127.0.0.1/aaatest.html

2、常用配置

2.1 主配置文件

#user www www;                                                    
#nginx进程数,建议设置为等于CPU总核心数。             
worker_processes  1;                                             
#工作模式与连接数上限                           
events {                              
    #单个进程最大连接数(最大连接数=连接数*进程数)         
    worker_connections  1024;         
}                                     
                                      
#设定http服务器                            
http {                                                                    
    #文件扩展名与文件类型映射表                    
    include       mime.types;         
                                 
    #默认文件类型                           
    default_type  application/octet-st
                                      
    error_log  logs/error.log;        
                                      
    sendfile        on;               
                                      
    #防止网络阻塞                           
    tcp_nopush     on;                
                                                                
    #长连接超时时间,单位是秒                     
    keepalive_timeout  65;            
                                      
    #开启gzip压缩输出                       
    gzip  on;                         
                                      
    # 把其他 server 配置写到其他文件,方便管理        
    include ../vhost/*.conf;          
}                                     

2.2 Server 配置

server {
    listen       80;
    server_name  localhost test.ycfeng.site;

    location / {
        root /root/frontend/testjenkins/;
        index index.html index.htm;
    }

    location /static/ {
        root /frontend/dist/;
    }
}

2.3 反向代理

server {
    listen 8182;
    server_name localhost;
    location / {
        proxy_pass http://localhost:8082; 
    }
 }

2.4 负载均衡配置

http {
    upstream myproject {
        server 127.0.0.1:8000 weight=3;
        server 127.0.0.1:8001;
        server 127.0.0.1:8002;
        server 127.0.0.1:8003;
    }

    server {
        listen 80;
        server_name www.domain.com;
        location / {
            proxy_pass http://myproject;
        }
    }
}

3、常见问题

上一篇下一篇

猜你喜欢

热点阅读