Skip to main content

4 posts tagged with "nginx"

View All Tags

php nginx File not found

· 2 min read

需求场景

最近在腾讯云重新部署php,nginx环境遇到一个非常坑的问题,两台服务器同样的配置,刚部署的服务器出现File not found.

排查

通过`tail -f /var/log/nginx/error.log`在日志中发现Permission denied,Primary script unknown 等报错,查找资料说是权限问题。一般都是让检查nginx的默认用户www-data和php-fpm的默认用户www-data是不是一致。因为默认都是www-data不去改动的话,肯定都是一样的。或者是项目目录权限改为777等,实测都是无效的。

最终解决方案

以为腾讯云默认用户为ubuntu,一般不推荐root直接登录。所以我是在ubuntu用户下登录部署的,把项目部署在了/home/ubuntu/projects下面。因为默认的/var/www/html是可以访问的,而我部署在/home/ubuntu/projects就是不可访问的,当我把项目移动至/home/projects下面就可以了。。。回想起来,之前的一台腾讯云服务器一直用root登录,项目部署在/home/projects也是没有问题的。

问题思考

所以这个问题的根本原因还是权限问题,只不过这个权限问题相当坑,777也解决不了。

微信h5(单页应用)不缓存html,缓存js,css等资源

· 2 min read

需求场景

在单页应用中,只有一个html页面,网站的大部分css,js等资源引用都写在这个html中,当更新css,js的随机数然后发布时,由于客户端缓存了html,并不能及时更新。

解决方案

    location / {
try_files $uri $uri/ /index.html;
if ($uri ~* .*\.(?:htm|html)$) {
add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
}
if ($uri ~* .*\.(?:js|css|jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm)$) {
expires 7d;
}
}

微信环境解决方案

微信环境中html的缓存也是一直遭人诟病的。在普通浏览器中,只需要将html后面也加一个随机数,就可以不缓存html,或者在meta标签中添加标识,又或者如上的nginx配置。而在微信浏览器中这些方法都是无效的。

 if ($uri ~* .*\.(?:htm|html)$) {
add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
add_header Last-Modified $date_gmt;
}

nginx上传文件出现413 Request Entity Too Large?

· One min read

需求场景

当用nginx上传文件时,出现413 Request Entity Too Large报错

解决方案

找到nginx的配置文件nginx.conf,修改或增加client_max_body_size属性值大小

http {

##
# Basic Settings
##

sendfile on;
tcp_nopush on;
tcp_nodelay on;
client_max_body_size 5m;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;

##以下省略
}

nginx域名泛解析绑定子目录配置

· 2 min read

需求场景

如果需要实现demo演示站。当然不希望每个小项目都去配置域名解析,太麻烦。所以用到了域名泛解析。并且将域名前缀和目录名字对应,根据目录名字即可访问对应的域名,就减少了很多配置。

具体配置(php示例)

server {
listen 80;
server_name ~^(?<subdomain>.+).demo.urcloud.co$;
root /home/demos/$subdomain;
index index.html index.htm index.php;
fastcgi_intercept_errors on;
error_page 404 = /404.html;
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't
# break when using query string
try_files $uri $uri/ =404;
}

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ .*\.php(\/.*)*$ {
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
include fastcgi.conf;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /.ht {
deny all;
}
}