亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 操作系統 > 正文

【nginx】常見的陷阱和錯誤

2024-06-28 14:33:09
字體:
來源:轉載
供稿:網友

很多人都可以碰到一個陷阱。下面我們列出,我們經常看到的問題,以及解釋如何解決這些問題。在Freenode上的#nginx IRC頻道這些討論很頻繁。

1、權限

從來不要使用777權限,查看目錄的權限

namei -om /path/to/check

2、root設置

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 /bar {        # [...]    }}

3、索引設置

BAD:

http {    index index.php index.htm index.html;    server {        server_name www.example.com;        location / {            index index.php index.htm index.html;            # [...]        }    }    server {        server_name example.com;        location / {            index index.php index.htm index.html;            # [...]        }        location /foo {            index index.php;            # [...]        }    }}

GOOD:

http {    index index.php index.htm index.html;    server {        server_name www.example.com;        location / {            # [...]        }    }    server {        server_name example.com;        location / {            # [...]        }        location /foo {            # [...]        }    }}

4、Using If

if 是邪惡的 參見 If Is Evil

5、Server Name (If)

BAD:

server {    server_name example.com *.example.com;        if ($host ~* ^www/.(.+)) {            set $raw_domain $1;            rewrite ^/(.*)$ $raw_domain/$1 permanent;        }        # [...]    }}

每次都要檢測主機頭,這是低效的,你應該避免,推薦使用下面的

GOOD:

server {    server_name www.example.com;    return 301 $scheme://example.com$request_uri;}server {    server_name example.com;    # [...]}

這樣方式便于閱讀,降低了nginx的處理要求,而且也避免了硬編碼(http or https)

6、Check (If) File Exists

使用if來判斷是可怕的,你應該使用 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 $uri/ /index.html;    }}

try_files 意味著你測試一個隊列 $uri => $uri/ => index.html,這種方法簡單,而且可以避免if

7、Web Apps中的控制器

Drupal, Joomla, etc. to work, just use this:

try_files $uri $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

一些軟件不需要 query string, 可以讀取 REQUEST_URI (例如,WordPress):

try_files $uri $uri/ /index.php;

如果你不關心目錄是否存在,你可以移除 $uri/

8、Passing Uncontrolled Requests to PHP

很多PHP網站中,配置nginx的例子中建議使用 .php (to the PHP interpretet)作為uri的結尾,這例有一個嚴重的安全問題對于大多數PHP程序,因為它可能允許執行任何第三方代碼

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/1232.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:
location ~* (file_a|file_b|file_c)/.php$ {    fastcgi_pass backend;    # [...]}
  • 在上傳目錄禁止執行任何PHP代碼
location /uploaddir {    location ~ /.php$ {return 403;}    # [...]}
  • 使用try_files指令過濾
location ~* /.php$ {    try_files $uri =404;    fastcgi_pass backend;    # [...]}
  • 使用嵌套位置過濾
location ~* /.php$ {    location ~ /..*/.*/.php$ {return 404;}    fastcgi_pass backend;    # [...]}

9、FastCGI Path in Script Filename

盡量使用 include fastcgi_params 中的變量,不管什么語言都是一樣

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

我們應該努力讓他們保持整潔。很簡單,不添加冗余代碼。

BAD:

rewrite ^/(.*)$ http://example.com/$1 permanent;

GOOD:

rewrite ^ http://example.com$request_uri? permanent;

BETTER:

return 301 http://example.com$request_uri;

通過使用內置的變量$ REQUEST_URI,我們可以有效地避免做任何捕獲或匹配的。

11、Rewrite Missing http://

很簡單,除非你告訴NGINX他們不是重寫是相對的。一個重寫絕對很簡單。添加一個scheme

BAD:

rewrite ^ example.com permanent;

GOOD:

rewrite ^ http://example.com permanent;

添加 http:// 到重寫規則內,簡單,高效

12、Proxy Everything

BAD:

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 to. 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 1MB 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 $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 $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、Config Changes Not Reflected

Browser 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、VirtualBox

If 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”. The directive is most likely found in your nginx.conf file.:

sendfile off;

13、Missing (disappearing) HTTP Headers

If you do not explicitly 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 Locations

Some directories in any file system should never be used for hosting data from. Some of these include / androot. You should 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 $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 should exist. You should 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 Root

NGINX 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 should 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 Addresses

BAD:

upstream {    server http://someserver;}server {    listen myhostname:80;    # [...]}

You should 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

由于SSLv3的POODLE 漏洞,建議使用在SSL網站禁用,僅僅使用TLS協議代替

ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 

原文:https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
欧美国产日产韩国视频| 欧美电影免费观看高清| 成人免费网视频| 欧美裸体xxxx极品少妇软件| 成人春色激情网| 精品视频在线播放色网色视频| 日本高清+成人网在线观看| 不卡在线观看电视剧完整版| 色播久久人人爽人人爽人人片视av| 欧美在线免费观看| 北条麻妃99精品青青久久| 不卡av日日日| 欧美成人一区在线| 亚洲另类激情图| 国产日产久久高清欧美一区| 日韩精品欧美激情| 亚洲一区二区三区777| 精品视频—区二区三区免费| 性金发美女69hd大尺寸| 亚洲一区二区三区乱码aⅴ| 91美女片黄在线观看游戏| 亚洲人成在线观| 欧美亚洲第一区| 美女少妇精品视频| 国产成人一区二区| 亚洲精品在线91| 日本sm极度另类视频| 日韩精品视频在线免费观看| 国产精品1234| 国产精品入口夜色视频大尺度| 国产欧美在线视频| 欧美日韩国产页| 精品亚洲一区二区三区四区五区| 日韩欧美高清视频| 欧美乱大交做爰xxxⅹ性3| 亚洲精品一区二区久| 亚洲精品自拍偷拍| 不卡毛片在线看| 欧美高跟鞋交xxxxxhd| 91中文精品字幕在线视频| 精品日本高清在线播放| 欧美激情精品久久久久久大尺度| 欧美乱大交做爰xxxⅹ性3| 欧美日韩xxxxx| 91精品成人久久| 亚洲欧美国内爽妇网| 欧美成人免费一级人片100| 岛国av一区二区在线在线观看| 久久香蕉精品香蕉| 欧美视频在线观看 亚洲欧| 日韩网站在线观看| 久久久久久亚洲精品中文字幕| 欧美精品九九久久| 韩国美女主播一区| 亚洲精品美女久久久久| 欧美成人激情视频| 欧美日韩国产麻豆| 欧美国产日韩免费| 久久久久久国产精品久久| 久久国产精品免费视频| 成人a视频在线观看| 另类视频在线观看| 日韩av免费在线看| 51视频国产精品一区二区| 国产精品96久久久久久| 国产精品欧美日韩| 欧美国产日韩精品| 亚洲人成电影网站色xx| 青草青草久热精品视频在线网站| 一区二区三区高清国产| 91精品国产91久久久久久吃药| 亚洲精品欧美日韩专区| 毛片精品免费在线观看| 欧美午夜片在线免费观看| 国产欧美日韩专区发布| 奇门遁甲1982国语版免费观看高清| 国产精品久久久久久一区二区| 欧美性xxxxx| 久久天堂av综合合色| 成人性生交大片免费看视频直播| 国产精品99久久99久久久二8| 中文字幕亚洲欧美日韩2019| 亚洲女人被黑人巨大进入al| 亚洲自拍偷拍视频| 日韩中文字幕视频| 成人自拍性视频| www亚洲精品| 2024亚洲男人天堂| 一级做a爰片久久毛片美女图片| 91美女高潮出水| 欧美日韩国产精品一区| 中文字幕欧美精品在线| 欧美亚洲在线观看| 日韩精品亚洲视频| 91亚洲国产成人精品性色| 欧美一级高清免费播放| 亚洲国产一区自拍| 国产精品极品美女在线观看免费| 亚洲人成在线播放| 欧美成人免费一级人片100| 欧美日韩国产区| 亚洲欧美一区二区三区久久| 欧美丰满老妇厨房牲生活| 欧美理论在线观看| 亚洲日韩第一页| 久久综合网hezyo| 欧美激情第一页xxx| 91爱视频在线| 欧美一级在线播放| 久久久久久久一区二区三区| 自拍偷拍免费精品| 国产精品69久久| 国产+成+人+亚洲欧洲| 麻豆精品精华液| 久久影院资源站| 亚洲高清免费观看高清完整版| 亚洲欧美国产一区二区三区| 久久精品小视频| 国产第一区电影| 亚洲第一视频网站| 国产91亚洲精品| 日韩精品黄色网| 亚洲精品久久7777777| 亚洲偷熟乱区亚洲香蕉av| 亚洲综合自拍一区| 欧美专区中文字幕| 亚洲精品国产拍免费91在线| 91精品久久久久久久久久久久久| 国产精品福利在线| 2019中文字幕免费视频| 国产精品久久久久久超碰| 亚洲国产日韩一区| 国产精品都在这里| 亚洲精品国产精品久久清纯直播| 伊人伊成久久人综合网小说| 欧美日本中文字幕| 91精品国产乱码久久久久久久久| 欧美在线观看视频| 欧美激情一区二区久久久| 国内精品国产三级国产在线专| 久久高清视频免费| 日韩av电影院| 久久精品国产96久久久香蕉| 中文字幕视频一区二区在线有码| 欧美视频二区36p| 欧美有码在线观看视频| 国产精品久久国产精品99gif| 68精品国产免费久久久久久婷婷| 欧美日韩国产二区| 98精品国产自产在线观看| 国产三级精品网站| 久久久午夜视频| 成人精品视频久久久久| 精品人伦一区二区三区蜜桃网站| 日韩高清av在线| 欧美限制级电影在线观看| 日韩影视在线观看| 在线电影欧美日韩一区二区私密| 欧美电影免费观看大全| 国产成人亚洲综合青青| 久久精品国产精品| 日韩精品一区二区三区第95| 高清一区二区三区日本久| 黑人巨大精品欧美一区二区免费|