安装

CentOS 7

  1. $ sudo yum -y install nginx # 安装 nginx
  2. $ sudo yum remove nginx # 卸载 nginx

配置路径:/etc/nginx

常用命令

  1. 启动:nginx
  2. 停止:nginx -s stop
  3. 重启:nginx -s reload
  4. 检查对配置文件的修改是否正确:nginx -t

ps:如果遇到nginx: [error] invalid PID number "" in "/run/nginx.pid"错误,试试nginx -c /etc/nginx/nginx.conf

设置白名单

在nginx的配置文件中,根据需要在http、server、location,任意级设置。

nginx直接对外

  1. allow 60.190.230.11;
  2. deny all;

经过SLB进入nginx

  1. set $allow false;
  2. if ($http_x_forwarded_for ~ "60.190.230.11"){
  3. set $allow true;
  4. }
  5. if ($allow = false){
  6. return 404;
  7. }

根据http_referer来跳转

  1. if ($http_referer ~* "www.baidu.com") {
  2. rewrite ^/(.*)$ http://www.lishiming.net redirect;
  3. }
  4. if ($http_referer ~* "www.google.com") {
  5. rewrite ^/(.*)$ http://www.lishiming.net redirect;
  6. }

前端单页面应用

  1. server {
  2. listen 80;
  3. # gzip config
  4. gzip on;
  5. gzip_min_length 1k;
  6. gzip_comp_level 9;
  7. gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
  8. gzip_vary on;
  9. gzip_disable "MSIE [1-6]\.";
  10. root /usr/share/nginx/html;
  11. include /etc/nginx/mime.types;
  12. location / {
  13. try_files $uri $uri/ /index.html;
  14. }
  15. }


常见问题

  1. 使用rewrite做http跳转https时,post请求变成了get
  2. nginx在线配置器