标签: Nginx

  • Nginx配置VUE应用路由重定向

    以下是使用 Nginx 来配置路由规则,使得 Vue 应用在打包后输入带有路由路径的 URL 能够正确显示的步骤:

    解决思路

    1. 配置 Nginx 以处理 Vue 应用的路由重定向。
    2. 确保所有的请求都被重定向到 index.html 文件,以便 Vue 的路由可以处理它们。

    配置文件示例

    收起

    nginx

    server {
        listen 80;
        server_name yourdomain.com;
    
        location / {
            root /path/to/your/vue/app/dist;
            index index.html;
            try_files $uri $uri/ /index.html;
        }
    }
    

    代码解释

    • listen 80;:Nginx 监听的端口,这里是 80 端口。
    • server_name yourdomain.com;:服务器的域名,你需要将 yourdomain.com 替换为你的实际域名。
    • location / {... }:这是一个 Nginx 的位置块,用于处理根路径 / 的请求。
    • root /path/to/your/vue/app/dist;:指定 Vue 应用的根目录,这里是 Vue 应用打包后的目录。
    • index index.html;:设置默认的首页文件为 index.html
    • try_files $uri $uri/ /index.html;:这个指令是关键。当用户请求一个文件时,Nginx 首先尝试查找 $uri 对应的文件,如果文件不存在,它会尝试查找 $uri/ 目录下的文件,如果还是不存在,它将请求重定向到 index.html。这样,对于 Vue 应用中的路由,当用户输入完整的路由路径时,Nginx 会将请求重定向到 index.html,让 Vue 的路由系统接管,从而避免 404 错误。

    详细步骤

    1. 打开 Nginx 的配置文件,通常位于 /etc/nginx/nginx.conf 或 /etc/nginx/conf.d/default.conf
    2. 将上述代码添加到 server 块中,根据你的实际情况修改 server_name 和 root 的值。
    3. 保存配置文件。
    4. 检查配置文件是否正确:sudo nginx -t
    5. 重新加载 Nginx 配置:sudo nginx -s reload

    注意事项

    • 确保 /path/to/your/vue/app/dist 是你 Vue 应用打包后的实际目录。
    • 确保你已经正确安装和启动了 Nginx。
    • 如果你的 Vue 应用使用的是子路径,比如 /app,你可以将 location / 改为 location /app {... },并相应地调整 root 路径和 try_files 指令。

    这样,当用户输入带有路由路径的 URL 时,Nginx 会将请求转发到 Vue 应用的 index.html,Vue 的路由系统会根据路由路径显示相应的内容,避免了 404 错误。

  • Nginx网站如何设置反向代理

    Nginx强大的正则表达式支持,可以使server_name的配置变得很灵活,如果你要做多用户博客,那么每个用户拥有自己的二级域名也就很容易实现了。

    下面我就来说说server_name的使用吧:

    server_name的匹配顺序

    Nginx中的server_name指令主要用于配置基于名称虚拟主机,server_name指令在接到请求后的匹配顺序分别为:

    1、准确的server_name匹配,例如:

    server {
    listen 80;
    server_name ssdr.info www.ssdr.info;

    }
    2、以*通配符开始的字符串:

    server {
    listen 80;
    server_name *.ssdr.info;

    }
    3、以*通配符结束的字符串:

    server {
    listen 80;
    server_name www.*;

    }
    4、匹配正则表达式:

    server {
    listen 80;
    server_name ~^(?.+)\.howtocn\.org$;

    }
    Nginx将按照1,2,3,4的顺序对server name进行匹配,只有有一项匹配以后就会停止搜索,所以我们在使用这个指令的时候一定要分清楚它的匹配顺序(类似于location指令)。 (更多…)

  • Nginx+FastCGI高性能WEB服务器

    【Nginx+php(FastCGI)+Memcached+Mysql+APC Nginx高性能web服务器安装 APC模块安装 linux apc 配置 fastCGI安装配置】

    下载所需的安装包:这里采用源码包编译安装:本博客集成下载
    http://wgkgood.gicp.net/download/nginx-0.7.61.tar.gz
    http://wgkgood.gicp.net/download/pcre-8.01.tar.gz
    http://wgkgood.gicp.net/download/memcache-2.2.5.tgz
    http://wgkgood.gicp.net/download/libevent-1.4.12-stable.tar.gz
    http://wgkgood.gicp.net/download/APC-3.1.4.tgz

    下载到 /usr/src下

    另外还有两个包mysql-5.1.41.tar.gz、php-5.3.5.tar.gz 【其他相似版本也可以!】可以在官网下载。

    一、正式安装Nginx、【安装nginx之前需要安装pcre包和zlib以支持重写,正则以及网页压缩等等】

    (1)首先安装pcre:

    cd  /usr/src  &&tar xzf pcre-8.01.tar.gz   &&cd pcre-8.01  &&  ./configure   –prefix=/usr/local/pcre  &&make &&make install

    (2)、然后再安装nginx :

    useradd www  && cd  /usr/src && tar xzf  nginx-0.7.61.tar.gz   &&cd  nginx-0.7.61  &&  ./configure –prefix=/usr/local/nginx –with-http_stub_status_module –with-openssl=/usr/ –with-pcre=/usr/src/pcre-8.01  –user=www –group=www  &&make  &&make install
    【nginx注意*  –with-pcre=/usr/src/pcre-8.01指向的是源码包解压的路径,而不是安装的路径,否则会报

    make[1]: *** [/usr/local/pcre/Makefile] Error 127 错误】
    二、接下来安装mysql

    cd   /usr/src && tar xzf  mysql-5.1.41.tar.gz   && cd mysql-5.1.41  && ./configure –prefix=/usr/local/mysql/ –enable-assembler –with-extra-charsets=complex –enable-thread-safe-client –with-big-tables –with-readline –with-ssl –with-embedded-server –enable-local-infile –with-plugins=innobase  && make  &&make install

    (2)、mysql安装完毕,创建mysql用户和组并初始化数据库,并启动数据库。

    cd  /usr/local/mysql && useradd mysql && chown -R  mysql:mysql   /usr/local/mysql  &&  /usr/local/mysql/bin/mysql_install_db  –user=mysql   &&   chown -R   mysql:mysql  var/  && ./bin/mysqld_safe   –user=mysql &

    即可。【如果mysql启动报错,请检查 /usr/local/mysql/var 目录,mysql是否有权限】

    三、安装 php :

    cd   /usr/src    &&tar xzf  php-5.3.5.tar.gz   && cd php-5.3.5  && ./configure –prefix=/usr/local/php –with-config-file-path=/usr/local/php/etc –with-mysql=/usr/local/mysql –with-mysqli=/u
    sr/local/mysql/bin/mysql_config –with-iconv-dir=/usr/local –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –with-li
    bxml-dir=/usr –enable-xml –disable-rpath –enable-discard-path –enable-safe-mode –enable-bcmath –enable-shmop –enable-sysvsem
    –enable-inline-optimization –with-curl –with-curlwrappers –enable-mbregex  –enable-fpm  –enable-sockets  && make  &&make install 

    安装完毕!【注意这个参数在此可以不加–enable-fastcgi;其他之前版本需要加上,以上安装根据自己的选择添加,如果报错,根据具体报错找原因】

    四、整合Nginx和php(FastCGI)安装完php-5.3.5后支持fastCGI

    (1)、配置nginx ,拷贝nginx配置文件:
    user  www www;

    worker_processes 8;

    error_log  /usr/local/logs/nginx/error.log  crit;

    pid        /usr/local/nginx/nginx.pid;

    #Specifies the value for maximum file descriptors that can be opened by this process.
    worker_rlimit_nofile 51200;

    events
    {
    use epoll;
    worker_connections 51200;
    }

    http
    {
    include       mime.types;
    default_type  application/octet-stream;

    #charset  gb2312;

    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 8m;

    sendfile on;
    tcp_nopush     on;

    keepalive_timeout 60;

    tcp_nodelay on;

    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;

    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;
    gzip_vary on;

    #limit_zone  crawler  $binary_remote_addr  10m;

    server
    {
    listen       80;
    server_name  wgkgood.gicp.net;
    index index.php index.htm index.html;
    root  /usr/webapps/www;

    #limit_conn   crawler  20;

    location ~ .*\.(php|php5)?$
    {
    #fastcgi_pass  unix:/tmp/php-cgi.sock;
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_index index.php;
    include fcgi.conf;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
    expires      30d;
    }

    location ~ .*\.(js|css)?$
    {
    expires      1h;
    }

    log_format  access  ‘$remote_addr – $remote_user [$time_local] “$request” ‘
    ‘$status $body_bytes_sent “$http_referer” ‘
    ‘”$http_user_agent” $http_x_forwarded_for’;
    access_log  /usr/local/logs/nginx/access.log  access;
    }

    }

    Nginx配置完毕!启动nginx ;/usr/local/nginx/sbin/nginx 即可,重启nginx命令如下/usr/local/nginx/sbin/nginx –s  reload

    此配置文件仅供参考,感谢张宴老师!

    (2)、配置fcgi.conf文件如下

    fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
    fastcgi_param  SERVER_SOFTWARE    nginx;

    fastcgi_param  QUERY_STRING       $query_string;
    fastcgi_param  REQUEST_METHOD     $request_method;
    fastcgi_param  CONTENT_TYPE       $content_type;
    fastcgi_param  CONTENT_LENGTH     $content_length;

    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
    fastcgi_param  REQUEST_URI        $request_uri;
    fastcgi_param  DOCUMENT_URI       $document_uri;
    fastcgi_param  DOCUMENT_ROOT      $document_root;
    fastcgi_param  SERVER_PROTOCOL    $server_protocol;

    fastcgi_param  REMOTE_ADDR        $remote_addr;
    fastcgi_param  REMOTE_PORT        $remote_port;
    fastcgi_param  SERVER_ADDR        $server_addr;
    fastcgi_param  SERVER_PORT        $server_port;
    fastcgi_param  SERVER_NAME        $server_name;

    # PHP only, required if PHP was built with –enable-force-cgi-redirect
    fastcgi_param  REDIRECT_STATUS    200;

    五、配置php配置文件:

    cd  /usr/local/php5/etc/ &&  cp

    php-fpm.conf.default   php-fpm.conf 然后根据提示修改php-fpm.conf里面的选项。

    配置完毕后,启动php-fpm

    cp  /usr/src/php-5.3.5/sapi/fpm/init.d.php-fpm  /etc/init.d/php-fpm 然后启动 /etc/init.d/php-fpm  start 即可。

    六、安装apc配置:

    cd  /usr/src && tar xzf  APC-3.1.4.tgz  &&cd  APC-3.1.4

    /usr/local/php5/bin/phpize  【增加扩展模块】

    ./configure –enable-apc –enable-apc-mmap –with-php-config=/usr/local/php5/bin/php-config

    &&make&& make install

    安装完后会生成一个apc.so在/usr/local/php5/lib/php/extensions/no-debug-non-zts-20060613/里面。

    七、安装memcached,使fastcGI支持memcached

    首先安装libevent,

    cd  /usr/src  && tar xzf libevent-1.4.12-stable.tar.gz  &&  cd  libevent-1.4.12-stable &&  ./configure –prefix=/usr/local/libevent &&make && make install

    然后安装memcached

    tar xzf  memcache-2.2.5.tar.gz  && cd memcache-2.2.5  && /usr/local/php5/bin/phpize  && ./configure –prefix=/usr/local/memcached  –with-libevent=/usr/local/libevent –with-php-config=/usr/local/php5/bin/php-config &&make  &&make install

    安装完后,会在/usr/local/php5/lib/php/extensions/no-debug-non-zts-20060613/里生成一个memcache.so 这个模块:

    八、接下来修改php.ini

    默认的php.ini在/usr/local/php5/lib/php.ini 你也可以指定:

    extension_dir = “./”

    修改为

    extension_dir=”/usr/local/php5/lib/php/extensions/no-debug-non-zts-20090626″

    把下面这些添加到最后:

    extension = apc.so

    extension=memcache.so //这里引用缓存模块

    [APC]

    apc.enabled = 1

    apc.shm_segments = 1

    apc.shm_size = 64M

    apc.optimization = 1

    apc.num_files_hint = 0

    apc.ttl=7200

    apc.user_ttl=7200

    apc.gc_ttl = 3600

    apc.cache_by_default = on

    安装到此已经完成!

  • nginx下防止被别人绑定域名

    两种解决方案:
    nginx 的默认虚拟主机在用户通过IP访问 ,或者通过未设置的域名 访问(比如有人把他自己的域名指向了你的ip)的时候生效。
    比如别人通过ip或者未知域名访问你的网站的时候,你希望禁止显示任何有效内容,可以给他返回500.
    目前国内很多机房都要求网站主关闭空主机头,防止未北岸的域名指向过来造成麻烦。
    就可以这样设置:
    1.直接返回500
    server {
    listen 80 default;
    return 500;
    }
    2. 也可以把这些流量 收集起来,导入到自己的网站,只要做以下跳转设置就可以:
    server {
    listen 80 default;
    rewrite ^(.*) http://pzg.me permanent;
    }
    两种解决方案:nginx 的默认虚拟主机在用户通过IP访问 ,或者通过未设置的域名 访问(比如有人把他自己的域名指向了你的ip)的时候生效。
    比如别人通过ip或者未知域名访问你的网站的时候,你希望禁止显示任何有效内容,可以给他返回500.   目前国内很多机房都要求网站主关闭空主机头,防止未北岸的域名指向过来造成麻烦。
    就可以这样设置:   1.直接返回500server {listen 80 default;return 500;}

    2. 也可以把这些流量 收集起来,导入到自己的网站,只要做以下跳转设置就可以:
    server {listen 80 default;rewrite ^(.*) http://pzg.me permanent;}

  • 2010年Web服务器排名

    在分析各个Web服务器之前,我们先来看看2010年Web的变化,下面是来自Netcraft的几个关键的调查数据:

    截止2010年底,互联网上有2.55亿站点;
    对比2009年,增长了9%,增加了2140万个站点。
    Web持续的快速增长,这将可能是个超过3000万或是更高的数字,如果不是去年的统计数据里面包括的QQ的博客。

    2010年Web服务器增长
    Web服务器的使用率的变化,如下:

    Apache 2009年托管1.09亿个站点,而2010年数字变成了1.52亿。
    Net result: 2010年,增加大约4300万新的Apache网站。
    Microsoft IIS 2009年托管4900万个站点,2010年是5700万。
    Net result: 2010年,大约新增800万个IIS网站。
    Nginx 没有2009年风光了,从1620万,增加到2010年的1690万。
    Google’s web server (最初服务博客) 从1410万,增加到2010年的1490万。
    Lighttpd 从84万,增加到130万。

    2010年市场变化
    根据上述数据,下面是2010年的市场份额变化:

    Apache 从46.6%到59.4%,收获12.8%。
    Microsoft IIS 从21.0%到22.2%,收获1.2%。
    Nginx 从6.6%到7.0%。
    Google’s web server 从6.0%到5.9%。
    Lighttpd 从0.4%到0.5%。

  • nginx下wp-super的rewrite规则

    location / {
    # if the requested file exists, return it immediately
                   if (-f $request_filename) {
                           break;
                   }
                   set $supercache_file ”;
                   set $supercache_uri $request_uri;
                   if ($request_method = POST) {
                           set $supercache_uri ”;
                   }
    # Using pretty permalinks, so bypass the cache for any query string
                   if ($query_string) {
                           set $supercache_uri ”;
                   }
                   if ($http_cookie ~* “comment_author_|wordpress|wp-postpass_” ) {
                           set $supercache_uri ”;
                   }
    # if we haven’t bypassed the cache, specify our supercache file
                   if ($supercache_uri ~ ^(.+)$) {
                           set $supercache_file /wp-content/cache/supercache/$http_host/

    $1index.html;
                   }
    # only rewrite to the supercache file if it actually exists
                   if (-f $document_root$supercache_file) {
                           rewrite ^(.*)$ $supercache_file break;
                   }
    # all other requests go to WordPress
                   if (!-e $request_filename) {
                           rewrite . /index.php last;
                   }

  • Nginx web server

    Nginx 是一个很牛的高性能Web和反向代理服务器, 它具有有很多非常优越的特性:

    在高连接并发的情况下,Nginx是Apache服务器不错的替代品: Nginx在美国是做虚拟主机生意的老板们经常选择的软件平台之一. 能够支持高达 50,000 个并发连接数的响应, 感谢Nginx为我们选择了 epoll and kqueue 作为开发模型.
    Nginx作为负载均衡服务器: Nginx 既可以在内部直接支持 Rails 和 PHP 程序对外进行服务, 也可以支持作为 HTTP代理 服务器对外进行服务. Nginx采用C进行编写, 不论是系统资源开销还是CPU使用效率都比 Perlbal 要好很多.
    作为邮件代理服务器: Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器), Last.fm 描述了成功并且美妙的使用经验.
    Nginx 是一个 [#installation 安装] 非常的简单 , 配置文件 非常简洁(还能够支持perl语法), Bugs 非常少的服务器: Nginx 启动特别容易, 并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动. 你还能够 不间断服务的情况下进行软件版本的升级 .