Nginx是一个反向代理服务器,用于不同的协议,如HTTPS、HTTP、POP3、SMTP和IMAP,以及HTTP缓存、负载均衡器和原始web服务器。作为一个开源项目,Nginx开始关注高性能、高并发性和低内存使用。Nginx可以在Mac OS X、Linux、BSD变体、Solaris、HP-UX、AIX和其他*nix版本上运行。此外,您还可以找到Windows概念端口的证明。该项目是根据类似bsd的2条款许可的,下面我们将为Magento 2 Nginx配置蒙上阴影。
要部署Nginx与动态HTTP内容交互,应该使用CGI协议(FastCGI、SCGI)、Web服务器网关接口或Phusion Passenger模块的不同替代方案。它还可以作为软件负载平衡器。
Nginx的发展始于2002年,成立于9年后的2011年7月。公司offerы商业支持自2012年2月,和支付2013年8月以来NGINX +订阅。
为了处理请求,Nginx依赖于事件驱动的异步方法,而不是Apache HTTP服务器模型的面向流程方法,以事件MPM作为异步处理模型。由于模块化事件驱动架构,Nginx的性能在更高的负载下更容易预测。
幸运的是,Magento 2可以轻松安装在Nginx上。您只需要在“服务器块”中实现以下配置设置。将此方法视为与Apache虚拟主机等效的方法。对于“example.com”网站,创建文件/etc/nginx/sites-available/example.com,添加以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 | upstream fastcgi_backend { server unix:/run/php-fpm/php-fpm.sock; } server { listen 80; server_name example.com; set $MAGE_ROOT /path/to/magento/root/dir/; set $MAGE_MODE default; include /path/to/magento/root/dir/nginx.conf.sample; } |
如果需要,您可以启用开发人员模式。只需使用以下变量:
1 2 | # Magento 2 Developer mode set $MAGE_MODE developer; |
Magento 2的Nginx配置(示例)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | # Magento Vars # set $MAGE_ROOT /path/to/magento/root; # set $MAGE_MODE default; # or production or developer # # Example configuration: # upstream fastcgi_backend { # # use tcp connection # # server 127.0.0.1:9000; # # or socket # server unix://var/run/php5-fpm.sock; # } # server { # listen 80; # server_name mage.dev; # set $MAGE_ROOT /var/www/magento2; # set $MAGE_MODE developer; # include /vagrant/magento2/nginx.conf.sample; # } root $MAGE_ROOT/pub; index index.php; autoindex off; charset off; add_header 'X-Content-Type-Options' 'nosniff'; add_header 'X-XSS-Protection' '1; mode=block'; location /setup { root $MAGE_ROOT; location ~ ^/setup/index.php { fastcgi_pass fastcgi_backend; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ ^/setup/(?!pub/). { deny all; } location ~ ^/setup/pub/ { add_header X-Frame-Options "SAMEORIGIN"; } } location /update { root $MAGE_ROOT; location ~ ^/update/index.php { fastcgi_split_path_info ^(/update/index.php)(/.+)$; fastcgi_pass fastcgi_backend; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; include fastcgi_params; } # deny everything but index.php location ~ ^/update/(?!pub/). { deny all; } location ~ ^/update/pub/ { add_header X-Frame-Options "SAMEORIGIN"; } } location / { try_files $uri $uri/ /index.php?$args; } location /pub { location ~ ^/pub/media/(downloadable|customer|import|theme_customization/.*\.xml) { deny all; } alias $MAGE_ROOT/pub; add_header X-Frame-Options "SAMEORIGIN"; } location /static/ { if ($MAGE_MODE = "production") { expires max; } location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ { add_header Cache-Control "public"; add_header X-Frame-Options "SAMEORIGIN"; expires +1y; if (!-f $request_filename) { rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last; } } location ~* \.(zip|gz|gzip|bz2|csv|xml)$ { add_header Cache-Control "no-store"; add_header X-Frame-Options "SAMEORIGIN"; expires off; if (!-f $request_filename) { rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last; } } if (!-f $request_filename) { rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last; } add_header X-Frame-Options "SAMEORIGIN"; } location /media/ { try_files $uri $uri/ /get.php?$args; location ~ ^/media/theme_customization/.*\.xml { deny all; } location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ { add_header Cache-Control "public"; add_header X-Frame-Options "SAMEORIGIN"; expires +1y; try_files $uri $uri/ /get.php?$args; } location ~* \.(zip|gz|gzip|bz2|csv|xml)$ { add_header Cache-Control "no-store"; add_header X-Frame-Options "SAMEORIGIN"; expires off; try_files $uri $uri/ /get.php?$args; } add_header X-Frame-Options "SAMEORIGIN"; } location /media/customer/ { deny all; } location /media/downloadable/ { deny all; } location /media/import/ { deny all; } location ~ cron\.php { deny all; } location ~ (index|get|static|report|404|503)\.php$ { try_files $uri =404; fastcgi_pass fastcgi_backend; fastcgi_param PHP_FLAG "session.auto_start=off \n suhosin.session.cryptua=off"; fastcgi_param PHP_VALUE "memory_limit=256M \n max_execution_time=600"; fastcgi_read_timeout 600s; fastcgi_connect_timeout 600s; fastcgi_param MAGE_MODE $MAGE_MODE; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } |
如何使用Nginx建立多个站点
首先,按照/ Nginx .conf.sample >中的建议设置Nginx配置。该文件由Magento软件提供。您应该得到一个小的配置文件(main.conf);基于magento提供的示例(nginx.magento.conf)创建的配置文件
现在,我们将解释如何配置两个网站——site1.store.com和site2.store.com。注意,多个主机只需要一个入口点。
对于$MAGE_RUN_TYPE变量,可以使用相同的方法在value store的帮助下运行多个商店,而不是使用website。
1.首先,您应该编写一个map指令来设置前面提到的$MAGE_RUN_CODE变量:
1 2 3 4 | map $http_host $MAGE_RUN_CODE { site1.store.com site1; site2.store.com site2; } |
1 | location ~ (index|get|static|report|404|503)\.php$ { ... } |
插入这两行:
1 2 3 4 | ... fastcgi_param MAGE_RUN_TYPE website; fastcgi_param MAGE_RUN_CODE $MAGE_RUN_MODE; ... |
1 | systemctl reload nginx |