Nginx/zh-tw: Difference between revisions

From Joomla! Documentation

Created page with "以下介紹如何在 [http://wiki.nginx.org/PHPFcgiExample nginx and PHP via FastCGI] 中使用 Joomla!"
FuzzyBot (talk | contribs)
Updating to match new version of source page
 
(11 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<noinclude><languages /></noinclude>
<noinclude><languages /></noinclude>
[http://nginx.net/ nginx] is a lightweight Web server that powers about 13% of Web servers across all domains[http://en.wikipedia.org/wiki/Nginx#Description]. Unless you have specific requirements that demand a heavy Web server like Apache, you are much better off using nginx.
<div lang="en" dir="ltr" class="mw-content-ltr">
[http://nginx.org/ Nginx] is a lightweight Web server that powers [https://en.wikipedia.org/wiki/Nginx about 33%] of Web servers across all domains. Unless you have specific requirements that demand a heavy Web server like Apache, you are much better off using Nginx.
</div>


以下介紹如何在 [http://wiki.nginx.org/PHPFcgiExample nginx and PHP via FastCGI] 中使用 Joomla!
<div class="mw-translate-fuzzy">
以下介紹如何在 [http://wiki.nginx.org/PHPFcgiExample nginx PHP FastCGI] 中使用 Joomla!
</div>


== Install nginx ==
<div class="mw-translate-fuzzy">
For Ubuntu, run <tt>aptitude install nginx</tt>. For other distros, run the corresponding package manager, or see http://wiki.nginx.org/Install.
== 安裝 nginx ==
</div>
<div class="mw-translate-fuzzy">
對於Ubuntu 的使用者,執行 <tt>aptitude install nginx</tt>。對於其他的Linux版本,請使用各自的套件管理器(Package Manager),或者請看 http://wiki.nginx.org/Install
</div>


== Install PHP FastCGI ==  
== 安裝 PHP FastCGI ==  
For Ubuntu, read this excellent page on how to [http://wiki.nginx.org/PHPFcgiExample configure PHP and FastCGI for nginx].
<div class="mw-translate-fuzzy">
對於 Ubuntu 的使用者,請看這篇非常詳細的如何 [http://wiki.nginx.org/PHPFcgiExample 設定 PHP 以及 FastCGI nginx] 的教學。
</div>


For Gentoo, PHP will run as a FastCGI service (fpm), so the nginx server will run PHP as a separated process:
<div class="mw-translate-fuzzy">
對於 Gentoo 的使用者,PHP 需要執行 FastCGI 的服務 (fpm),所以 nginx 的服務將會分開執行 PHP
</div>
<pre>
<pre>
# echo "dev-lang/php gd gd2 curl simplexml tokenizer dom tidy sqlite xml fpm cgi" >> /etc/portage/package.use
# echo "dev-lang/php gd gd2 curl simplexml tokenizer dom tidy sqlite xml fpm cgi" >> /etc/portage/package.use
# emerge php
# emerge php
</pre>
</pre>
The default settings of php-fpm are good for most servers. For special configurations, visit the [http://php.net/manual/en/install.fpm.php PHP FPM site].
<div lang="en" dir="ltr" class="mw-content-ltr">
The default settings of PHP-FPM are good for most servers. For special configurations, visit the [http://php.net/manual/en/install.fpm.php PHP FPM site].
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
== Configure Nginx ==
== Configure Nginx ==
nginx configuration files reside in:
</div>
* <tt>/etc/nginx/sites-available/</tt> on Ubuntu (for sites running on that nginx instance)
<div lang="en" dir="ltr" class="mw-content-ltr">
* <tt>/etc/nginx/nginx.conf</tt> on Gentoo and Raspbian(= Debian optimized for Raspberry Pi)
Nginx configuration files reside in:
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
* <tt>/etc/nginx/sites-available/</tt> on Ubuntu (for sites running on that Nginx instance)
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
* <tt>/etc/nginx/nginx.conf</tt> on Gentoo and Raspbian (Debian optimized for Raspberry Pi)
</div>


Here is an sample nginx configuration file, joomla.conf, that you can reuse over all your nginx enabled-sites.
<div lang="en" dir="ltr" class="mw-content-ltr">
Here is an sample Nginx configuration file, ''joomla.conf'', that you can reuse over all your Nginx enabled-sites.
</div>


<pre>
<pre>
server {
server {
        listen 80;
  listen 80;
        server_name YOUR_DOMAIN;
    server_name YOUR_DOMAIN;
        server_name_in_redirect off;
    server_name_in_redirect off;


        access_log /var/log/nginx/localhost.access_log;
    access_log /var/log/nginx/localhost.access_log;
        error_log /var/log/nginx/localhost.error_log info;
    error_log /var/log/nginx/localhost.error_log info;


        root PATH_ON_SERVER;
    root PATH_ON_SERVER;
        index index.php index.html index.htm default.html default.htm;
    index index.php index.html index.htm default.html default.htm;
        # Support Clean (aka Search Engine Friendly) URLs
        location / {
                try_files $uri $uri/ /index.php?$args;
        }


        # deny running scripts inside writable directories
    # Support API
        location ~* /(images|cache|media|logs|tmp)/.*\.(php|pl|py|jsp|asp|sh|cgi)$ {
    location /api/ {
                return 403;
try_files $uri $uri/ /api/index.php?$args;
                error_page 403 /403_error.html;
    }
        }


        location ~ \.php$ {
    # Support Clean (aka Search Engine Friendly) URLs
            fastcgi_pass  127.0.0.1:9000;
    location / {
            fastcgi_index index.php;
        try_files $uri $uri/ /index.php?$args;
            include fastcgi_params;
    }
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include /etc/nginx/fastcgi.conf;
        }


        # caching of files
    # add global x-content-type-options header
        location ~* \.(ico|pdf|flv)$ {
    add_header X-Content-Type-Options nosniff;
                expires 1y;
        }


         location ~* \.(js|css|png|jpg|jpeg|gif|swf|xml|txt)$ {
    # deny running scripts inside writable directories
                expires 14d;
    location ~* /(images|cache|media|logs|tmp)/.*\.(php|pl|py|jsp|asp|sh|cgi)$ {
        }
        return 403;
        error_page 403 /403_error.html;
    }
 
    location ~ \.php$ {
      fastcgi_pass  127.0.0.1:9000;
      fastcgi_index index.php;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include /etc/nginx/fastcgi.conf;
    }
 
    # caching of files
    location ~* \.(ico|pdf|flv)$ {
         expires 1y;
    }
 
    location ~* \.(js|css|png|jpg|jpeg|gif|swf|xml|txt)$ {
        expires 14d;
    }


}
}
</pre>
</pre>


<div lang="en" dir="ltr" class="mw-content-ltr">
Pay attention to a few things:
Pay attention to a few things:
# The parameter <tt>fastcgi_pass</tt> is set to 127.0.0.1:9000, corresponding to the port that fpm is configured to listen to. This means you can run the PHP processes on separate servers. On Gentoo, you can find this configuration in <tt>/etc/php/fpm-php5.3/php-fpm.conf/</tt>
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
# The parameter ''fastcgi_pass'' is set to ''127.0.0.1:9000'', corresponding to the port that FPM is configured to listen to. This means you can run the PHP processes on separate servers. On Gentoo, you can find this configuration in the ''/etc/php/fpm-php5.3/php-fpm.conf/'' file.
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
# Don't forget to replace YOUR_DOMAIN & PATH_ON_SERVER above depending on your domain and the path of Joomla on your server.
# Don't forget to replace YOUR_DOMAIN & PATH_ON_SERVER above depending on your domain and the path of Joomla on your server.
</div>


== GZip support ==
 
If you need GZip compression support, add the following section to the http section of the main nginx configuration file:
<div lang="en" dir="ltr" class="mw-content-ltr">
== GZip Support ==
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
If you need GZip compression support, add the following section to the ''http'' section of the main Nginx configuration file:
</div>
<pre>
<pre>
        gzip on;
gzip on;
        gzip_http_version 1.1;
gzip_http_version 1.1;
        gzip_comp_level 6;
gzip_comp_level 6;
        gzip_min_length 1100;
gzip_min_length 1100;
        gzip_buffers 4 8k;
gzip_buffers 4 8k;
        gzip_types text/plain application/xhtml+xml text/css application/xml application/xml+rss text/javascript application/javascript application/x-javascript
gzip_types text/plain application/xhtml+xml text/css application/xml application/xml+rss text/javascript application/javascript application/x-javascript
        gzip_proxied     any;
gzip_proxied any;
        gzip_disable     "MSIE [1-6]\.";
gzip_disable "MSIE [1-6]\.";
</pre>
</pre>


<div lang="en" dir="ltr" class="mw-content-ltr">
=== Module ngx_http_gzip_static_module ===
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
The '''ngx_http_gzip_static_module''' module allows sending precompressed files with the “.gz” filename extension instead of regular files. The directive enables Nginx to stream directly the pre-gzipped files that Joomla offers. It means less processing power is needed and the files are delivered with a far better compression ratio.
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
To use it, update the gzip parameters in the ''http'' section of the main Nginx configuration file:
</div>
<pre>
gzip on;
gzip_static on;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain application/xhtml+xml text/css application/xml application/xml+rss text/javascript application/javascript application/x-javascript
gzip_proxied any;
gzip_disable "MSIE [1-6]\.";
</pre>
<div lang="en" dir="ltr" class="mw-content-ltr">
'''Note:''' This module is not built by default, it should be enabled with the '''--with-http_gzip_static_module''' configuration parameter. Refer to [https://nginx.org/en/docs/http/ngx_http_gzip_static_module.html ngx_http_gzip_static_module] and [https://nginx.org/en/docs/configure.html Building Nginx from Sources]
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
== Sources ==
== Sources ==
* [http://en.gentoo-wiki.com/wiki/Nginx Nginx in Gentoo]
</div>
* [http://www.kevinworthington.com/nginx-for-windows/ Nginx for Windows]
* [https://wiki.gentoo.org/wiki/Nginx Nginx in Gentoo]
* [http://wiki.nginx.org/Install#Ubuntu_PPA Nginx in Ubuntu]
* [https://kevinworthington.com/nginx-for-windows/ Nginx for Windows]
* [http://www.debianadmin.com/howto-install-nginx-webserver-in-debian.html Nginx in Debian]
* [https://ubuntu.com/tutorials/install-and-configure-nginx#1-overview Nginx in Ubuntu]
* [http://php.net/manual/en/install.fpm.php PHP-FPM installation and configuration]
* [https://www.debianadmin.com/howto-install-nginx-webserver-in-debian.html Nginx in Debian]
* [http://wiki.nginx.org/HttpGzipModule GZip in Nginx]
* [https://www.php.net/manual/en/install.fpm.php PHP-FPM installation and configuration]
* [http://wiki.nginx.org/HttpRewriteModule Rewrite in Nginx]
* [https://docs.nginx.com/nginx/admin-guide/web-server/compression/ Compression and Decompression]
* [http://nginx.org/en/docs/http/request_processing.html How nginx processes a request]
* [https://www.nginx.com/blog/creating-nginx-rewrite-rules/ Creating NGINX Rewrite Rules]
* [http://nginx.org/en/docs/http/request_processing.html How Nginx processes a request]
 
[[Category:Server configurations]]

Latest revision as of 15:02, 23 July 2024

Nginx is a lightweight Web server that powers about 33% of Web servers across all domains. Unless you have specific requirements that demand a heavy Web server like Apache, you are much better off using Nginx.

以下介紹如何在 nginx 和 PHP FastCGI 中使用 Joomla!

對於Ubuntu 的使用者,執行 aptitude install nginx。對於其他的Linux版本,請使用各自的套件管理器(Package Manager),或者請看 http://wiki.nginx.org/Install。

安裝 PHP FastCGI

對於 Ubuntu 的使用者,請看這篇非常詳細的如何 設定 PHP 以及 FastCGI 給 nginx 的教學。

對於 Gentoo 的使用者,PHP 需要執行 FastCGI 的服務 (fpm),所以 nginx 的服務將會分開執行 PHP:

# echo "dev-lang/php gd gd2 curl simplexml tokenizer dom tidy sqlite xml fpm cgi" >> /etc/portage/package.use
# emerge php

The default settings of PHP-FPM are good for most servers. For special configurations, visit the PHP FPM site.

Configure Nginx

Nginx configuration files reside in:

  • /etc/nginx/sites-available/ on Ubuntu (for sites running on that Nginx instance)
  • /etc/nginx/nginx.conf on Gentoo and Raspbian (Debian optimized for Raspberry Pi)

Here is an sample Nginx configuration file, joomla.conf, that you can reuse over all your Nginx enabled-sites.

server {
  listen 80;
    server_name YOUR_DOMAIN;
    server_name_in_redirect off;

    access_log /var/log/nginx/localhost.access_log;
    error_log /var/log/nginx/localhost.error_log info;

    root PATH_ON_SERVER;
    index index.php index.html index.htm default.html default.htm;

    # Support API
    location /api/ {
	try_files $uri $uri/ /api/index.php?$args;
    }

    # Support Clean (aka Search Engine Friendly) URLs
    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    # add global x-content-type-options header
    add_header X-Content-Type-Options nosniff;

    # deny running scripts inside writable directories
    location ~* /(images|cache|media|logs|tmp)/.*\.(php|pl|py|jsp|asp|sh|cgi)$ {
        return 403;
        error_page 403 /403_error.html;
    }

    location ~ \.php$ {
      fastcgi_pass  127.0.0.1:9000;
      fastcgi_index index.php;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include /etc/nginx/fastcgi.conf;
    }

    # caching of files 
    location ~* \.(ico|pdf|flv)$ {
        expires 1y;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|swf|xml|txt)$ {
        expires 14d;
    }

}

Pay attention to a few things:

  1. The parameter fastcgi_pass is set to 127.0.0.1:9000, corresponding to the port that FPM is configured to listen to. This means you can run the PHP processes on separate servers. On Gentoo, you can find this configuration in the /etc/php/fpm-php5.3/php-fpm.conf/ file.
  1. Don't forget to replace YOUR_DOMAIN & PATH_ON_SERVER above depending on your domain and the path of Joomla on your server.


GZip Support

If you need GZip compression support, add the following section to the http section of the main Nginx configuration file:

gzip on;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain application/xhtml+xml text/css application/xml application/xml+rss text/javascript application/javascript application/x-javascript
gzip_proxied any;
gzip_disable "MSIE [1-6]\.";

Module ngx_http_gzip_static_module

The ngx_http_gzip_static_module module allows sending precompressed files with the “.gz” filename extension instead of regular files. The directive enables Nginx to stream directly the pre-gzipped files that Joomla offers. It means less processing power is needed and the files are delivered with a far better compression ratio.

To use it, update the gzip parameters in the http section of the main Nginx configuration file:

gzip on;
gzip_static on;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain application/xhtml+xml text/css application/xml application/xml+rss text/javascript application/javascript application/x-javascript
gzip_proxied any;
gzip_disable "MSIE [1-6]\.";

Note: This module is not built by default, it should be enabled with the --with-http_gzip_static_module configuration parameter. Refer to ngx_http_gzip_static_module and Building Nginx from Sources


Sources