ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

Nginx常见问题

2022-06-24 18:02:27  阅读:156  来源: 互联网

标签:index 常见问题 name server Nginx code root fastcgi


Nginx常见问题

nginx多server优先级

# 优先级匹配顺序
1.首先选择所有的字符完全匹配 (精确匹配) 的server_name。 (完全匹配)

2.选择通配符在前面的server_name

3.选择通配符在后面的server_name

4.正则表达式的server_name、

5.所有匹配规则相同时,哪个配置文件listen...后面加了default哪个优先级就最高

6.按照匹配文件的顺序访问第一个配置文件


禁止IP访问

# 禁止IP访问,并访问错误页面

server {
        listen 80 default_server;
        server_name _;
        charset utf-8;
        default_type text/json;
        return 500 "页面500 ,给爷爬~";
}
server{
        listen 80;
        server_name blog.zh.com;
        root /code/wordpress;
        index index.php index.html;

        location ~ \.php$ {
                fastcgi_pass unix:/dev/shm/php.sock;
                fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
                #fastcgi_param HTTPS on;
                include fastcgi_params;
        }
}

# 禁止Ip访问并跳转到主页
server {
        listen 80 default_server;
        server_name _;
        charset utf-8;
        rewrite (.*) http://blog.zh.com$1 redirect;
}
server{
        listen 80;
        server_name blog.zh.com;
        root /code/wordpress;
        index index.php index.html;

        location ~ \.php$ {
                fastcgi_pass unix:/dev/shm/php.sock;
                fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
                #fastcgi_param HTTPS on;
                include fastcgi_params;
        }
}

Nginx包含其他子配置文件

一台服务器配置多个网站,如果配置都写在nginx.conf主配置文件中,会导致nginx.conf主配置文件变得非常庞大而且可读性非常的差。那么后期的维护就变得麻烦。 假设现在希望快速的关闭一个站点,该怎么办? 
1.如果是写在nginx.conf中,则需要手动注释,比较麻烦 
2.如果是include的方式,那么仅需修改配置文件的扩展名,即可完成注释 Include包含的作用是为了简化主配置文件,便于人类可读。

站点目录路径root和alias区别

# root指定站点目录
server{
        listen 80;
        server_name img.zh.com;
        root /code/2;
        index index.html;

        incation /images{
                root /code/images;
        }
}

# 图片路径:/code/images/images/1.png
# alias指定站点目录
server{
        listen 80;
        server_name img.zh.com;
        root /code/2;
        index index.html;

        incation /images{
                alias /code/images;
        }
}
# 图片路径 :/code/images/1.png

Nginx try_file路径匹配

server {
        listen 80 default_server;
        server_name _;
        charset utf-8;
        rewrite (.*) http://www.baidu.com$1 redirect;
}

server{
        listen 80;
        server_name blog.zh.com;
        root /code/wordpress;
        index index.php index.html;

        location ~ \.php$ {
                fastcgi_pass unix:/dev/shm/php.sock;
                fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
                #fastcgi_param HTTPS on;
                include fastcgi_params;
        }
# 路径匹配
        location / {
                try_files $uri $uri/  zh;
        }
        location zh {
                proxy_pass http://blog.zh.com;
        }
}

nginx调整上传大小

Syntax: client_max_body_size size; 
Default: client_max_body_size 1m; 
Context: http, server, location

nginx优雅显示404错误页面

server {
        listen 80;
        server_name www.zh.com;

        location /{
                root /code;
                index index.html;
                error_page 404  /404.html;
        }
}

隐藏版本号

http{
	server_tokens off; 
	... 
}

标签:index,常见问题,name,server,Nginx,code,root,fastcgi
来源: https://www.cnblogs.com/ghjhkj/p/16409803.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有