安装
CentOS 7
$ sudo yum -y install nginx # 安装 nginx$ sudo yum remove nginx # 卸载 nginx
配置路径:
/etc/nginx
常用命令
- 启动:nginx
- 停止:nginx -s stop
- 重启:nginx -s reload
- 检查对配置文件的修改是否正确:nginx -t
ps:如果遇到nginx: [error] invalid PID number "" in "/run/nginx.pid"错误,试试nginx -c /etc/nginx/nginx.conf。
设置白名单
在nginx的配置文件中,根据需要在http、server、location,任意级设置。
nginx直接对外
allow 60.190.230.11;deny all;
经过SLB进入nginx
set $allow false;if ($http_x_forwarded_for ~ "60.190.230.11"){set $allow true;}if ($allow = false){return 404;}
根据http_referer来跳转
if ($http_referer ~* "www.baidu.com") {rewrite ^/(.*)$ http://www.lishiming.net redirect;}if ($http_referer ~* "www.google.com") {rewrite ^/(.*)$ http://www.lishiming.net redirect;}
前端单页面应用
server {listen 80;# gzip configgzip on;gzip_min_length 1k;gzip_comp_level 9;gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;gzip_vary on;gzip_disable "MSIE [1-6]\.";root /usr/share/nginx/html;include /etc/nginx/mime.types;location / {try_files $uri $uri/ /index.html;}}
