[Nginx] common traps and errors

Source: Internet
Author: User
Tags sendfile drupal

[Nginx] common traps and errors
Many people may encounter a trap. The following lists the problems we often see and explains how to solve them. The # nginx IRC channel on Freenode is frequently discussed. 1. Do not use the 777 permission for permissions. view the directory permission 1 namei-om/path/to/check2, and set root to BAD: server {server_name www.example.com; location/{root/var/www/nginx-default/; # [...]} location/foo {root/var/www/nginx-default/; # [...]} location/bar {root/var/www/nginx-default/; # [...]} GOOD: server {server_name www.example.com; root/var/www/nginx-default/; location/{# [...]} location/foo {# [...]} location/ba R {# [...]} 3. index setting BAD: http {index. php index.htm index.html; server {server_name www.example.com; location/{index. php index.htm index.html; # [...]} server {server_name example.com; location/{index. php index.htm index.html; # [...]} location/foo {index. php; # [...]} GOOD: http {index. php index.htm index.html; server {server_name www.example.com; Location/{# [...]} server {server_name example.com; location/{# [...]} location/foo {# [...]} 4. Using Ifif Is Evil. See If Is edevil 5, Server Name (If) BAD: server {server_name example.com * .example.com; if ($ host ~ * ^ Www \. (. +) {set $ raw_domain $1; rewrite ^ /(. *) $ raw_domain/$1 permanent;} # [...]} check the Host Header every time. This is inefficient. You should avoid this. We recommend using the following GOOD: server {server_name www.example.com; return 301 $ scheme: // example.com $ request_uri ;} server {server_name example.com; # [...]} this method is easy to read and reduces nginx processing requirements, and avoids hard encoding (http or https) 6 and Check (If) File Exists using if to determine whether it is terrible, you should use try_files BAD: server {root/var/www/example.com; location /{If (! -F $ request_filename) {break ;}} GOOD: server {root/var/www/example.com; location/{try_files $ uri // index.html ;}} try_files means that you test a queue uri => uri/=> index.html. This method is simple and can avoid the controllers Drupal, Joomla, and etc in if 7 and Web Apps. to work, just use this: try_files $ uri // index. php? Q = $ uri & $ args; Note-the parameter names are different based on the package you're using. for example: "q" is the parameter used by Drupal, Joomla, WordPress "page" is used by CMS Made Simple some software do not need query string, you can read REQUEST_URI (For example, WordPress): try_files $ uri // index. php; if you do not care whether the directory exists, you can remove $ uri/8, Passing Uncontrolled Requests to PHP from many PHP websites. We recommend that you use this in the nginx configuration example. php (to the PHP interpretet) as the uri end In this example, there is a serious security problem for most PHP programs, because it may allow execution of any third-party code The problem section usually looks like this: location ~ *\. Php $ {fastcgi_pass backend; # [...]} here, every request ending in. php will be passed to the FastCGI backend. the issue with this is that the default PHP configuration tries to guess which file you want to execute if the full path does not lead to an actual file on the filesystem. for instance, if a request is made for/forum/avatar/1232.jpg/ file. php which does not exist but if/forum/avatar/12 32. jpg does, the PHP interpreter will process/forum/avatar/1232.jpg instead. if this contains embedded PHP code, this code will be executed accordingly. options for avoiding this are: Set cgi. fix_pathinfo = 0 in php. ini. this causes the PHP interpreter to only try the literal path given and to stop processing if the file is not found. ensure that NGINX only passes specific PHP files for execution: l Ocation ~ * (File_a | file_ B | file_c) \. php $ {fastcgi_pass backend; # [...]} the upload directory prohibits execution of any PHP Code location/uploaddir {location ~ \. Php $ {return 403;} # [...]} use the try_files command to filter location ~ * \. Php $ {try_files $ uri = 404; fastcgi_pass backend; # [...]} filter location ~ by nested location ~ * \. Php $ {location ~ \.. */. *\. Php $ {return 404;} fastcgi_pass backend; # [...]} 9. Try to use the variables in include fastcgi_params in FastCGI Path in Script Filename. No matter which language is the same, GOOD: fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name; BAD: fastcgi_param SCRIPT_FILENAME/var/www/yoursite.com/?fastcgi_script_name=10?taxing Rewrites we should try to keep them clean. It is very simple. No redundant code is added. BAD: rewrite ^/(. *) $ http://example.com/#1 permanent; GOOD: rewrite ^ http://example.com $ request_uri? Permanent; BETTER: return 301 http://example.com $ request_uri; by using the built-in variable $ REQUEST_URI, we can effectively avoid any capture or matching. 11. Rewrite Missing http: // very simple, unless you tell NGINX that they are not overwritten. Rewriting is absolutely simple. Add a scheme BAD: rewrite ^ example.com permanent; GOOD: rewrite ^ http://example.com permanent; add http: // to rewrite rules, simple, efficient 12, Proxy EverythingBAD: server {server_name _; root/var/www/site; location/{include fastcgi_params; fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name; fastcgi_pass unix:/tmp/phpcgi. socket ;}} Yucky. in this instance, you pass EVERYTHING to PHP. why? Apache might do this, you don't need. let me put it this way... the try_files directive exists for an amazing reason. it tries files in a specific order. this means that NGINX can first try to server the static content. if it can't, then it moves on. this means PHP doesn' t get involved at all. MUCH faster. especially if you're serving a 1 MB image over PHP a few thousand times versus serving it Directly. let's take a look at how to do that. GOOD: server {server_name _; root/var/www/site; location/{try_files $ uri/@ proxy;} location @ proxy {include fastcgi_params; fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name; fastcgi_pass unix:/tmp/phpcgi. socket ;}} Also GOOD: server {server_name _; root/var/www/site; location/{try_files $ uri // index. php;} location ~ \. Php $ {include fastcgi_params; fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name; fastcgi_pass unix:/tmp/phpcgi. socket ;}} It's easy, right? You see if the requested URI exists and can be served by NGINX. if not, is it a directory that can be served. if not, then you pass it to your proxy. only when NGINX can't serve that requested URI directly does your proxy overhead get involved. now .. consider how much of your requests are static content, such as images, css, javascript, etc. that's probably a lot of overhead you just saved. 12. Con Fig Changes Not reflectedbrow.cache. your configuration may be perfect but you'll sit there and beat your head against a cement wall for a month. what's wrong is your browser cache. when you download something, your browser stores it. it also stores how that file was served. if you are playing with a types {} block you'll encounter this. the fix: In Firefox press Ctrl + Shift + Delete, check Cache, Click Clear Now. in any other browser just ask your favorite search engine. do this after every change (unless you know it's not needed) and you'll save yourself a lot of headaches. use curl.13, VirtualBoxIf this does not work, and you're running NGINX on a virtual machine in VirtualBox, it may be sendfile () that is causing the trouble. simply comment out the sendfile directive or set it to "off ". T He directive is most likely found in your nginx. conf file.: sendfile off; 13. Missing (disappearing) HTTP HeadersIf you do not explain itly set underscores_in_headers on, NGINX will silently drop HTTP headers with underscores (which are perfectly valid according to the HTTP standard ). this is done in order to prevent ambiguities when mapping headers to CGI variables as both dashes and underscores are Mapped to underscores during that process. 14. Not Using Standard Document Root LocationsSome directories in any file system shoshould never be used for hosting data from. some of these include/androot. you shoshould never use these as your document root. doing this leaves you open to a request outside of your expected area returning private data. never do this !!! (Yes, we have seen this) server {root/; location/{try_files/web/$ uri @ php;} location @ php {[...]} when a request is made for/foo, the request is passed to php because the file isn' t found. this can appear fine, until a request in made for/etc/passwd. yup, you just gave us a list of all users on that server. in some cases, the NGINX server is even set up run workers as root. yup, we now have your user list as well as password hashes and how they 've been hashed. we now own your box. the Filesystem Hierarchy Standard defines where data shoshould exist. you shoshould definitely read it. the short version is that you want your web content to exist in either/var/www/,/srv,/usr/share/www. 15. Using the Default Document RootNGINX packages that exist in Ubuntu, Debian, or other operating systems, as an easy-to-install package will often provide a 'default' configuration file as an example of configuration methods, and will often include a document root to hold a basic HTML file. most of these packaging systems do not check to see if files are modified or exist within the default document root, which can result in code loss when the packages are upgraded. experienced system administrators know that there is no expectation of the data inside the default document root to remain untouched during upgrades. you shoshould not use the default document root for any site-critical files. there is no expectation that the default document root will be left untouched by the system and there is an extremely high possibility that your site-critical data may be lost upon updates and upgrades to the NGINX packages for your operating system. 16. Using a Hostname to Resolve AddressesBAD: upstream {server http: // someserver;} server {listen myhostname: 80; # [...]} you shoshould never use a hostname in a listen directive. while this may work, it will come with a large number of issues. one such issue being that the hostname may not resolve at boot time or during a service restart. this can cause NGINX to be unable to bind to the desired TCP socket which will prevent NGINX from starting at all. A safer practice is to know the IP address that needs to be bound to and use that address instead of the hostname. this prevents NGINX from needing to look up the address and removes dependencies on external and internal resolvers. this same issue applies to upstream locations. while it may not always be possible to avoid using a hostname in an upstream block, it is bad practice and will require careful considerations to prevent issues. GOOD: upstream {server http: // 10.48.41.12;} server {listen 127.0.0.16: 80; # [...]} 17. Using SSLv3 with HTTPS due to the POODLE vulnerability in SSLv3, we recommend that you disable it on the SSL website. Instead of 1ssl_protocols TLSv1 TLSv1.1 TLSv1.2, use TLS Protocol only;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.