ubuntu之nginx的安装

1.sudo apt-get install nginx
2.nginx -v(查看版本号)

相关链接: http://blog.takwolf.com/2016/10/19/setup-nginx-on-ubuntu/index.html
https://www.cnblogs.com/gabrielchen/p/5066120.html


关于关闭阿帕奇

 在/usr/local/apache2/bin 

 1. 启动 ./httpd

 2. 重启 ./apachectl restart

 关闭80端口

 通过 lsof -i:80 查看不了PID值

 sudo fuser -k 80/tcp (关闭80端口)

 如何通过nginx设置虚拟主机

  1. 在腾讯云备案添加二级域名

   2. cd /etc/nginx, 修改nginx.conf 

 

    把原先的配置去掉,以后要添加虚拟主机都在vhost文件夹下添加

   比如

server{
        listen 80;
        server_name blog.guiqingl.cn;
        location /
        {
                proxy_pass http://127.0.0.1:3000/;
                proxy_set_header Host $host:$server_port;
                proxy_set_header X-Real_IP $remote_addr;
		proxy_set_header REMOTE-HOST $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_connect_timeout 300s;
                proxy_read_timeout 300s;
                proxy_send_timeout 300s;
                proxy_redirect default;
		#禁止Scrapy等工具的抓取
                if ($http_user_agent ~* (Scrapy|Curl|HttpClient)) {
                        return 403;
                }
                #禁止指定UA及UA为空的访问
                if ($http_user_agent ~ "FeedDemon|JikeSpider|Indy Library|Alexa Toolbar|AskTbFXTV|AhrefsBot|CrawlDaddy|CoolpadWebkit|Java|Feedly|UniversalFeedParser|ApacheBench|Microsoft URL Control|Swiftbot|ZmEu|oBot|jaunty|Python-urllib|lightDeckReports Bot|YYSpider|DigExt|YisouSpider|HttpClient|MJ12bot|heritrix|EasouSpider|LinkpadBot|Ezooms|^$" )
                {
                        return 403;
                }
                if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot|360spider"){
                        return 403;
                }
                #禁止非GET|HEAD|POST方式的抓取
                if ($request_method !~ ^(GET|HEAD|POST)$) {
                        return 403;
                }
        }
        #access_log /home/wwwlogs/7pomsproxy.log access;
}
这就是我的一个文件的配置
对于静态文件的配置
server {
        listen 80;
        server_name www.guiqingl.cn;
        root  /usr/local/apache2/htdocs;
       # access_log  /home/wwwlogs/uco2H5.log access;
        index index.html index.php;

        location ~ .*.php$ {
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME    /home/wwwroot/current/uco2H5/$fastcgi_script_name;
                fastcgi_pass 127.0.0.1:9000;
        }
        location / {
        if (!-e $request_filename) {
                rewrite ^/Interface/index.php/(.*)$ /Interface/index.php?s=$1 last;
                rewrite ^/index.php/(.*)$ /index.php?s=$1 last;
                break;
            }
    }
        location ~* .*.(js|css)?&
        {
                expires 1h;
                access_log off;
        }

        location ~* .*.(png|jpg|gif|jpeg|bmp|ico)?$
        {
                expires 1h;
                access_log off;
        }

        location ~* .*.(zip|rar|exe|msi|iso|gho|ppt|mp3|rmvb|mp4|wma|wmv|rm)?$
        {
                expires 1h;
                access_log off;
        }
}

 还有就是解决跨域问题的配置文件

server {
        listen 80;
        server_name www.guiqingl.cn;
        root  /usr/local/apache2/htdocs/world-cup/dist;
        index index.html index.php;

      #处理vue-router路径Start
      #如果找不到路径则跳转到@router变量中寻找,找到了就默认进入index.html
      location / {
        try_files $uri $uri/ /index.html last;
        index index.html;
      }

        location ^~/proxyPrefix/
        {
            rewrite ^/proxyPrefix/(.*)$ /$1 break;
            proxy_pass http://127.0.0.1:8081/;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real_IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_connect_timeout 300s;
            proxy_read_timeout 300s;
            proxy_send_timeout 300s;
            proxy_redirect default;
            #禁止Scrapy等工具的抓取
            if ($http_user_agent ~* (Scrapy|Curl|HttpClient)) {
                    return 403;
            }
            #禁止指定UA及UA为空的访问
            if ($http_user_agent ~ "FeedDemon|JikeSpider|Indy Library|Alexa Toolbar|AskTbFXTV|AhrefsBot|CrawlDaddy|CoolpadWebkit|Java|Feedly|UniversalFeedParser|ApacheBench|Microsoft URL Control|Swiftbot|ZmEu|oBot|jaunty|Python-urllib|lightDeckReports Bot|YYSpider|DigExt|YisouSpider|HttpClient|MJ12bot|heritrix|EasouSpider|LinkpadBot|Ezooms|^$" )
            {
                    return 403;
            }
            if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot|360spider"){
                    return 403;
            }
            #禁止非GET|HEAD|POST方式的抓取
            if ($request_method !~ ^(GET|HEAD|POST)$) {
                    return 403;
            }
        }

        location ~* .*.(js|css)?&
        {
                expires 1h;
                access_log off;
        }

        location ~* .*.(png|jpg|gif|jpeg|bmp|ico)?$
        {
                expires 1h;
                access_log off;
        }

        location ~* .*.(zip|rar|exe|msi|iso|gho|ppt|mp3|rmvb|mp4|wma|wmv|rm)?$
        {
                expires 1h;
                access_log off;
        }
}

配置完后,需要重启Nginx

在/etc/nginx目录下

关闭 sudo nginx -s quit
启动 sudo nginx

重启 sudo nginx -s reload

 nginx 开启 静态文件压缩功能

 1.目前个人项目都是通过compression依赖性来压缩静态资源

 2. 通过nginx配置项进行静态文件压缩

 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-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
 gzip_vary off;
 gzip_disable "MSIE [1-6].";

 3、解释一下

 第1行:开启Gzip

 第2行:不压缩临界值,大于1K的才压缩,一般不用改

 第3行:buffer,就是,嗯,算了不解释了,不用改

 第4行:用了反向代理的话,末端通信是HTTP/1.0,有需求的应该也不用看我这科普文了;有这句的话注释了就行了,默认是HTTP/1.1

 第5行:压缩级别,1-10,数字越大压缩的越好,时间也越长,看心情随便改吧

 第6行:进行压缩的文件类型,缺啥补啥就行了,JavaScript有两种写法,最好都写上吧,总有人抱怨js文件没有压缩,其实多写一种格式就行了

 第7行:跟Squid等缓存服务有关,on的话会在Header里增加"Vary: Accept-Encoding",我不需要这玩意,自己对照情况看着办吧

 第8行:IE6对Gzip不怎么友好,不给它Gzip了

原文地址:https://www.cnblogs.com/luguiqing/p/7858654.html