Nginx的配置(进阶):https://segmentfault.com/a/11...javascript
$ yum install nginx
咕咕咕php
$ mv /var/log/nginx/access.log /var/log/nginx/20180816.log $ kill -USER1 Nginx主进程号 # 让Nginx从新生成一个新的日志文件access.log
EPEL 指的是 Extra Packages for Enterprise Linux,由 Fedora 社区维护,专门给 RHEL 系的操做系统使用,而且相对于 CentOS 默认的仓库,更新比较快。
Remi 是基于 EPEL 的针对 PHP 打包的仓库,更新也很及时。css
$ yum install epel-release
若是VPS商家的系统是精简的:html
$ yum install http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
安装Remi仓房:java
$ yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
接着更新一下系统而且安装一些必要的软件:mysql
$ yum update $ yum install curl vim wget sudo unzip yum-utils
指定 PHP 包的版本nginx
$ yum-config-manager --enable remi-php70 $ yum update
安装一些基本的PHP包:git
$ yum install php-fpm php-mysql php-curl php-gd php-mbstring php-mcrypt php-xml php-xmlrpc php-zip
修改一下/etc/php.ini
sql
$ sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/' /etc/php.ini
编辑Nginx的配置文件vim
$ vim /etc/nginx/nginx.conf server { listen 80 default_server; listen [::]:80 default_server; server_name _; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { } [*] 在此处添加便可 # 开启PHP-fpm 模式 location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } }
重启php和nginx
$ systemctl restart php-fpm $ systemctl restart nginx
参考连接:https://sb.sb/blog/centos-ins...
~ #波浪线表示执行一个正则匹配,区分大小写 ~* #表示执行一个正则匹配,不区分大小写 ^~ #^~表示普通字符匹配,若是该选项匹配,只匹配该选项,不匹配别的选项,通常用来匹配目录 = #进行普通字符精确匹配 @ #"@" 定义一个命名的 location,使用在内部定向时,例如 error_page, try_files
例如:
location / { index index.php index.html; error_page 404 =200 /404.html; }
查看配置文件,确保包括了default.d
目录下的配置文件
http { ... include /etc/nginx/default.d/*.conf; ... }
灵活配置,能够针对不一样server
作不一样的访问控制。
而后在default.d
目录下建立访问控制列表。
假如这里要添加黑名单,那就建立black.conf
:
$ cat black.conf deny 123.151.43.110;
deny
表示禁止,支持通配符和正则。
location / { autoindex on; }
$ vim /etc/nginx/nginx.conf http { gzip on; # 开启Gzip gzip_min_length 1k; # 不压缩临界值,大于1K的才压缩 gzip_buffers 4 16k; #gzip_http_version 1.1; gzip_comp_level 2; # 压缩级别,1-10,数字越大压缩的越好,时间也越长 gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; # 压缩文件类型 #gzip_vary on; # 跟Squid等缓存服务有关 gzip_disable "MSIE [1-6]\."; # 不压缩IE6 ... }
... proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off; server { set $upstream http://ip:port location / { proxy_cache my_cache; proxy_pass $upstream; } } ...
$ vim /etc/nginx/nginx.conf ... location /htdocs { alias /opt/www/alias; index index.html; } ...
alias
只会匹配最右侧的路径。
例如输入http://www.brownfly.cn/htdocs/index.html
,那么匹配的则是/opt/www/alias/index.html
,
而不是/opt/www/alias/htdocs/index.html
。
基于http_refer防盗链配置模块:
$ vim /etc/nginx/nginx.conf ... location ~* .*\.(git|png|jpg|jpeg|swf|fle)$ { valid_referers none blocke 127.0.0.1 *.baidu; # 容许文件链出的域名白名单 if ($invalid_referer){ #rewrite ^/ http://118.25.89.91/404.html; # 盗链返回结果 return 403; }
身份验证可用于一些私密目录。
$ yum install -y httpd-tools $ cd /etc/nginx/conf.d $ htpasswd -c -m .htpasswd http1 # 建立http1用户 输入密码 $ htpasswd -m .htpasswd http2 # 建立http2用户 输入密码
$ vim /etc/nginx/nginx.conf location /secret { auth_basic "test site"; auth_basic_user_file /etc/nginx/conf.d/.htpasswd; }