Nginx常用详解

设置多个虚拟主机

nginx.conf 配置中心 添加如下配置,则vhost目录下的conf配置文件则为每个单独的虚拟主机

include vhost/*.conf;

在这里插入图片描述
每个虚拟主机都可以做单独设置,如下

server {
    listen       8004;
    server_name  vr-local;
    root /Users/zhujiule/Downloads/cqkj/workspace/test/www/;

    location / {
        index  index.html index.htm index.php;
        if (!-e $request_filename) {  
           rewrite  ^/(.*)$  /index.php/$1  last;
           break;  
        } 
    }

    location ~ \.php {
        #fastcgi_pass   127.0.0.1:9000;
        fastcgi_pass   unix:/tmp/php73-cgi.sock;
        fastcgi_index  index.php;
        include fastcgi.conf;
        
        fastcgi_buffer_size 128k;
        fastcgi_buffers 32 32k;

        set $real_script_name $fastcgi_script_name;
        if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
            set $real_script_name $1;
            set $path_info $2;
        }
        fastcgi_param  SCRIPT_FILENAME $document_root$real_script_name;
        fastcgi_param  SCRIPT_NAME $real_script_name;
        fastcgi_param  PATH_INFO $path_info;
    }
}

基本功能

配置404的错误链接指向

错误指向

# 在root配置目录下创建40x.html 加入内容
error_page 404 /40x.html;
# 亦可以404时指向外部链接
# error_page 404 htts://www.baidu.com/

location = /40x.html {
}

精准匹配

只允许部分ip访问

location ~\.php$ {
   allow 192.168.74.129;
   deny all;
}

反向代理

为服务器做代理(可能代理到不同的服务器上), 反向代理跟代理正好相反现在基本所有的大型网站的页面都是用了反向代理,客户端发送的请求,想要访问 server 服务器上的内容。发送的内容被发送到代理服务器上,这个代理服务器再把请求发送到自己设置好的内部服务器上,而用户真实想获得的内容就在这些设置好的服务器上。

  • 提高了安全性(攻击的只是代理服务器)
  • 功能性, 提供负载均衡
  • 缓存功能, 减小服务器压力
server {
    listen       3000;    //监听的本地端口
    server_name  localhost;    
                
    location /api {         //匹配到/api开头的接口时,转发到下面的服务器地址
        root   html;  
        proxy_pass  http://192.168.xxx.xxx:8080;    //服务器地址      
    }  
                        
    location =/ {
        root html;
        index  index.htm  index.html;   //默认主页
    }
                        
    # 所有静态请求都由nginx处理,存放目录为html  
    location ~ \.(htm|html|js|css|jpg|png|gif|eot|svg|ttf|woff|woff2)$ {  
        root    html;      //配置静态资源地址
    }    
                
    error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        root   html;
    }
}

动静分离

动静分离是为了减少不必要的请求已减少资源的浪费、请求的延时。

    # 所有静态请求都由nginx处理,存放目录为html  
    location ~ \.(htm|html|js|css|jpg|png|gif|eot|svg|ttf|woff|woff2)$ {  
        root    html;      //配置静态资源地址
    }    
                

https配置

如果需要使用https则可以按照如下进行配置

server {
    listen       443 ssl;    //监听443端口
    server_name  www.domain.com;    //配置域名
                
    ssl_certificate      证书的绝对路径;
    ssl_certificate_key  密钥的绝对路径;
                
    # location / {        //反向代理的服务器地址,视情况进行配置
    #    proxy_pass   http://112.35.xxx.xxx;
    # }          
}   

适配pc和移动端

通过user-agent来区分移动端和pc

server{
  listen 80;
  server_name nginx.confidence.com;
  location / {
    # 默认展示pc目录下 index.html
    root /opt/app/code/pc;

    # 通过用户代理系统应用, 如果是移动端系统展示移动端网页
    if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
      root /opt/app/code/mobile;
    }
    index index.html;
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值