分类 Nginx 下的文章

方法一

报错提示一般是多了个空格,将新加的配置多余空格删除即可。

方法二

若配置文件确实没问题,可能conf文件被记事本编辑过,保存成了含[BOM] 。所以才报错的。

解决办法
使用editplus 等其它非记事本 另存为 UTF-8 不含BOM 的文件就可以了。

nginx unknown directive

nginx在反向代理HTTP协议的时候,默认使用的是HTTP1.0去向后端服务器获取响应的内容后在返回给客户端。
HTTP1.0和HTTP1.1的一个不同之处就是,HTTP1.0不支持HTTP keep-alive。nginx在后端服务器请求时使用了HTTP1.0同时使用HTTP Header的Connection:Close通知后端服务器主动关闭连接。这样会导致任何一个客户端的请求都在后端服务器上产生了一个TIME-WAIT状态的连接。所以我们需要在Nginx上启用HTTP1.1的向后端发送请求,同时支持Keep-alive。

配置方法

我们增加三个参数keepalive 50,proxy_http_version 1.1 , proxy_set_header Connection 来配置。
upstream http_backend {

server 127.0.0.1:8080;
keepalive 50;

}
server {

...
location /http/ {
    proxy_pass http://http_backend;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    ...
}

}

如以下的配置是 所有静态页面都到/my/web/item/m路径下,排除ueditor编辑器的html路径

location ~ /ueditor/.*\.(html)$   #需要排除的路径放在这里,不然走下面的html 404找不到路径
{
    root  /my/web/item;
}

location ~* \.(html)$ {  #访问静态页面
         expires 30d;  #缓存30天
         root /my/web/item/m;
}

注意事项:

排除的放上面,优先级高。

解决方法,代理的属性上加上端口配置server_port
proxy_set_header Host $host:$server_port;

     #proxy_set_header        Host $host;
     #nginx非80端口处理 加上 :$server_port
     proxy_set_header   Host $host:$server_port;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_redirect off;
     client_max_body_size 10m;      
     client_body_buffer_size 128k;  
     proxy_connect_timeout 90;      
     proxy_read_timeout 90;         
     proxy_buffer_size 4k; 
     proxy_buffers 6 32k; 
     proxy_busy_buffers_size 64k;  
     proxy_temp_file_write_size 64k; 

Nginx动静分离核心配置

  #动静分离
  server{
    listen 80;
    server_name cc.com;
    access_log off;

    location ~* \.(png|jpg|js|css)$ {
        proxy_pass http://127.0.0.1:8080;
        #所有以.png .html .js .css结尾的url进入此路径  (png|html|js|css)
    }

    location /{
         index index.html index.htm;
         proxy_pass http://127.0.0.1:8080/m/;#注意这里的/的含义见下文
    }

  }

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

完整配置参考

server {
        listen       80;
        server_name   aaa.com;
    
        #access_log  logs/web.log  main;
        #access_log  off;
        location ~ ^/(WEB-INF)/ {  
            deny all;  
        }
     #rewrite ^(.*)$  https://$host$1 permanent; #用于将http页面重定向到https页面

     #动静分离
     location ~* \.(gif|jpg|png|bmp|swf|js|css)$ {
        expires 30d;  #缓存30天
        proxy_pass http://localhost:8080;
        #所有以.png .html .js .css结尾的url进入此路径  (png|html|js|css)
     }
     
     location / {           
         root   /cycy/web/site/m;

         index index.jsp index.html index.htm;
         proxy_pass   http://localhost:8080/m/;
         proxy_set_header   Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_redirect off;
         client_max_body_size 10m;      
         client_body_buffer_size 128k;  
         proxy_connect_timeout 90;      
         proxy_read_timeout 90;         
         proxy_buffer_size 4k;
         proxy_buffers 6 32k;
         proxy_busy_buffers_size 64k;  
         proxy_temp_file_write_size 64k;     
    }
    
        gzip on;
        gzip_min_length 1k;
        gzip_buffers    4 16k;
        gzip_http_version 1.0;
        gzip_comp_level 2;
        gzip_types text/plain application/x-javascripttext/css application/xml;
        gzip_vary on;
        #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;
        #}
    }
    

扩展阅读

基本语法: location [=|~|~*|^~]/uri/{...}
= 严格匹配, 如果这个查询匹配,将停止搜索并立即处理此请求
~ 区分大小写匹配(可用正则表达式)
~* 不区分大小写匹配(可用正则表达式)
!~ 区分大小写匹配
!~* 不区分大小写匹配
^~ 如果把这个前缀用于一个常规字符串
免责声明
本博客部分内容来自于互联网,不代表作者的观点和立场,如若侵犯到您的权益,请联系[email protected]。我们会在24小时内进行删除。