无法理解NGINXconfiguration

我正在处理的Magento应用程序中遇到403&404错误,我在NGINXconfiguration中将这个问题追查到了两个块,如果我将它们解决了,问题就解决了,但是我想更好地理解我正在评论的内容。

问题1

我在url上遇到了403错误,例如

http://www.example.com/media/wysiwyg/.thumbs/wysiwyg/banner.jpg 

我猜这是由于下面的代码块&当然,当我评论它的工作!

 # Deny all attempts to access hidden files # such as .htaccess, .htpasswd, etc... location ~ /\. { deny all; access_log off; log_not_found off; } 

现在的问题是我的.gitignore文件现在可以访问。 我怎样才能更好地重写这个块? 什么~ /\. 意思?

问题2

我在url上遇到了404错误,例如:

 http://www.example.com/js/gene/braintree/braintree-0.1.js 

我发现,如果我改变了这个文件的名字braintree-0.1.min.js我停止了对它的404错误,当我从NGINX删除下面的块,它的原始名称加载罚款, braintree-0.1.js所以它必须与文件名后面的点有关。

 ## # Rewrite for versioned CSS+JS via filemtime ## location ~* ^.+\.(css|js)$ { rewrite ^(.+)\.(\d+)\.(css|js)$ $1.$3 last; expires 31536000s; access_log off; log_not_found off; add_header Pragma public; add_header Cache-Control "max-age=31536000, public"; } 

我不太确定这个块在做什么或者是什么~* ^.+\.(css|js)$意思是,我确定我把它推荐给了那些推荐它的人。 不知道它在做什么?

下面是我的完整的NGINXconfiguration文件,在此先感谢您的任何帮助和build议,你可能有:)

服务器{#监听端口80以及发送443 SSL连接。 听8080; #listen 443 default ssl;

  server_name www.example.com; # Specify path to your SSL certificates. #ssl_certificate /etc/nginx/certificates/yourcertificate.crt; #ssl_certificate_key /etc/nginx/certificates/yourcertificate.key; # Path to the files in which you wish to # store your access and error logs. #access_log /path/to/your/logs/access_log; #error_log /path/to/your/logs/error_log; # If the site is accessed via mydomain.com # automatically redirect to www.magento.localhost.com. #if ($host = 'example' ) { #rewrite ^/(.*)$ http://www.example/$1permanent; #} root /var/www/example/; auth_basic "Restricted website - authorised access only"; auth_basic_user_file /etc/nginx/.htpasswd; location / { index index.html index.htm index.php; try_files $uri $uri/ @handler; } #include hhvm.conf; # INCLUDE HHVM HERE # Deny access to specific directories no one # in particular needs access to anyways. location /app/ { deny all; } location /includes/ { deny all; } location /lib/ { deny all; } location /media/downloadable/ { deny all; } location /pkginfo/ { deny all; } location /report/config.xml { deny all; } location /var/ { deny all; } # Allow only those who have a login name and password # to view the export folder. Refer to /etc/nginx/htpassword. #location /var/export/ { # auth_basic "Restricted"; # auth_basic_user_file htpasswd; # autoindex on; #} location ~* /magmi($|/) { auth_basic "Restricted website - authorised access only"; auth_basic_user_file /etc/nginx/.htpasswd; location ~ \.php$ { if (!-e $request_filename) { rewrite / /index.php last; } expires off; # --PHP5-FPM CONFIG START (keep fastcgi_param HTTPS OFF)-- #fastcgi_pass unix:/var/run/php5-fpm.sock; ##fastcgi_param HTTPS $fastcgi_https; #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # --PHP5-FPM CONFIG START-- # --HHVM CONFIG START-- fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # include fastcgi_params; try_files $uri $uri/ @handler; # --HHVM CONFIG END-- fastcgi_param MAGE_RUN_CODE default; fastcgi_param MAGE_RUN_TYPE store; include fastcgi_params; } } # Deny all attempts to access hidden files # such as .htaccess, .htpasswd, etc... location ~ /\. { deny all; access_log off; log_not_found off; } # This redirect is added so to use Magentos # common front handler when handling incoming URLs. location @handler { rewrite / /index.php; } # Forward paths such as /js/index.php/x.js # to their relevant handler. location ~ .php/ { rewrite ^(.*.php)/ $1 last; } ## # Rewrite for versioned CSS+JS via filemtime ## location ~* ^.+\.(css|js)$ { rewrite ^(.+)\.(\d+)\.(css|js)$ $1.$3 last; expires 31536000s; access_log off; log_not_found off; add_header Pragma public; add_header Cache-Control "max-age=31536000, public"; } ## # Aggressive caching for static files # If you alter static files often, please use # add_header Cache-Control "max-age=31536000, public, must-revalidate, proxy-revalidate"; ## location ~* \.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|ogv|otf|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|t?gz|tif|tiff|ttf|wav|webm|wma|woff|wri|xla|xls|xlsx|xlt|xlw|zip)$ { expires 31536000s; access_log off; log_not_found off; add_header Pragma public; add_header Cache-Control "max-age=31536000, public"; } # Handle the exectution of .php files. location ~ .php$ { if (!-e $request_filename) { rewrite / /index.php last; } expires off; # --PHP5-FPM CONFIG START (keep fastcgi_param HTTPS OFF)-- #fastcgi_pass unix:/var/run/php5-fpm.sock; ##fastcgi_param HTTPS $fastcgi_https; #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # --PHP5-FPM CONFIG START-- # --HHVM CONFIG START-- fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #include fastcgi_params; try_files $uri $uri/ @handler; # --HHVM CONFIG END-- fastcgi_param MAGE_RUN_CODE default; fastcgi_param MAGE_RUN_TYPE store; include fastcgi_params; } } 

什么〜/。 意思?

这当然是在许多教程中解释:

https://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms

官方文档:

http://nginx.org/en/docs/beginners_guide.html

http://nginx.org/en/docs/http/request_processing.html

〜表示正则expression式,并不完全匹配。 /。 意味着一个逃脱的点,所以是一个字面意思,而不是正则expression式中的“任何字符”。 这匹配.thumb匹配.htaccess和.gitignore

我怎样才能更好地重写这个块?

通过让它匹配你想要它匹配。 例如,如果你只关心.gitignore,你可以这样做:

 ~ /\.gitignore 

我不太确定这个块在做什么或者什么〜* ^。+。(css | js)$意思是,我确定我只是把它推荐给了那些推荐它的人。 不知道它在做什么?

当然,如果你花一点时间来理解正则expression式,你也会这样。

你现在知道〜意味着正则expression式如下。 星号表示正则expression式不区分大小写。 其余的可以通过许多方便的在线工具来解释,例如:

https://regex101.com/

你把你的正则expression式放在那里,和你匹配的string,你的JSurl。 它会告诉你:

^断言在string的开始位置

。+匹配任何字符(除了换行符)

量词:+在一次和无限次之间,尽可能多地按照需要回馈[贪婪]

。 匹配字符。 按照字面

第一捕获组(css | js)

第一select:

css css字符匹配字符css(区分大小写)

第二个select:js

js字面匹配字符js(区分大小写)

string结尾处的$ assert位置

你可能也想问一下如何改进那个。 这取决于你想要达到的目标。

这里的教训是:

1)学习一些RegExp 2)不要只是“把它从推荐它的某个人的博客中拿走”这在IT世界是危险的。

不要随意的博客,尝试坚持(有点)官方文档:

https://wiki.magento.com/display/m1wiki/Configuring+nginx+for+Magento+1.x

https://github.com/magenx/nginx-config/blob/master/magento/nginx.conf

Magento 2: https : //github.com/magento/magento2/blob/develop/nginx.conf.sample