Nginx常见问题

Nginx常见问题

1.nginx多server的优先级

# 优先级匹配顺序
1.首选所有的字符串完全匹配(精确匹配)的server_name
2.选择通配符在前面的server_name
3.匹配通配符在后面的server_name
4.正则表达式的server_name
5.多个配置文件相同,哪个配置文件的listen 后面加了default_server哪个优先级最高
6.所有配置文件都匹配不上时,就会按照配置文件的顺序访问第一个配置文件

2.禁止IP访问

1.禁止IP访问,返回错误界面
# 修改配置文件
[root@web01 ~]# vim /etc/nginx/conf.d/wordpress.jl.com.conf 

server {
        listen 80 default_server;
        server_name _;
        charset utf-8;
        default_type text/json;
        return 404 "页面不存在 404 NOT";
}
server {
        listen 80;
        server_name wordpress.jl.com;
        root /code/wordpress;
        index index.php index.html;
        access_log  /var/log/nginx/wordpress.log  main;

        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
systemctl reload nginx
# 浏览器访问

2.禁止IP访问并跳转到主站点
# 修改配置文件
server {
        listen 80 default_server;
        server_name _;
        charset utf-8;
        rewrite (.*) http://wordpress.jl.com$1 redirect;
}
server {
        listen 80;
        server_name wordpress.jl.com;
        root /code/wordpress;
        index index.php index.html;
        access_log  /var/log/nginx/wordpress.log  main;

        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
systemctl reload nginx
# 浏览器访问

3.站点目录路径root和alias区别

# 修改配置文件
server {
        listen 80 default_server;
        server_name _;
        charset utf-8;
        rewrite (.*) http://wordpress.jl.com$1 redirect;
}
server {
        listen 80;
        server_name wordpress.jl.com;
        root /code/wordpress;
        index index.php index.html;
        access_log  /var/log/nginx/wordpress.log  main;

        location /images {
                root /code/images;
        }
	# root图片路径 /code/images/images/xxx.png
        location /image {

                alias /images;
        }

	# alias图片路径 /code/images/xxx.png  
        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;
        }
}

4.Nginx try_file路径匹配

# 编写配置文件
[root@web01 ~]# vim /etc/nginx/conf.d/wordpress.jl.com.conf 

server {
        listen 80 default_server;
        server_name _;
        charset utf-8;
        rewrite (.*) http://wordpress.jl.com$1 redirect;
}
server {
        listen 80;
        server_name wordpress.jl.com;
        root /code/wordpress;
        index index.php index.html;
        access_log  /var/log/nginx/wordpress.log  main;
       ## nginx会按顺序检查文件及目录是否存在,如果第一个文件没有找到就会找第二个目录如果两个都没有就会执行内部重定向,跳转到命令的最后一个uri参数定义的URI中
        location / {
                try_files $uri $uri/ @jl
        }

        location @jl {
                proxy_pass http://172.16.1.8:80;
        }

        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;
        }
}

5.nginx调整上传文件的大小

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

6.nginx配置显示404错误页面

# 跳转页面
server {
        listen 80 default_server;
        server_name _;
        charset utf-8;
        rewrite (.*) http://wordpress.jl.com$1 redirect;
}
server {
        listen 80;
        server_name wordpress.jl.com;
        root /code/wordpress;
        index index.php index.html;
        access_log  /var/log/nginx/wordpress.log  main;
        # 当页面出现404时,跳转到百度
        error_page 404 https://www.baidu.com;

        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;
        }
}
# 浏览器访问

# 部署前端页面
server {
        listen 80 default_server;
        server_name _;
        charset utf-8;
        rewrite (.*) http://wordpress.jl.com$1 redirect;
}
server {
        listen 80;
        server_name wordpress.jl.com;
        root /code/wordpress;
        index index.php index.html;
        access_log  /var/log/nginx/wordpress.log  main;
        # 当页面出现404时,显示编写好的前端页面
        error_page 404 /404.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;
        }
}
# 浏览器访问

7.隐藏nginx版本号

在http层加入 "server_tokens off;" 即可
浏览器访问错误页面也会隐藏版本号

posted on   jilei  阅读(77)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示