nginx配置-最后整理版

nginx配置
  • 错误日志记录
  • 日志json格式
  • stub_status & 开启认证
  • 404错误页配置,并重定向
  • 某些后缀文件拒绝访问(default.conf)
  • 配置include(简化)
worker_processes auto;
worker_rlimit_nofile 65535;
error_log stderr notice;
error_log /var/log/nginx/error.log;

events {
    multi_accept on;
    use epoll;
    worker_connections 51200;
}

http {
    include                       mime.types;
    default_type                  application/octet-stream;
    server_name_in_redirect       off;
    client_max_body_size          20m;
    client_header_buffer_size     16k;
    large_client_header_buffers 4 16k;
    sendfile                      on;
    tcp_nopush                    on;
    keepalive_timeout             65;
    server_tokens                 off;
    gzip                          on;
    gzip_min_length               1k;
    gzip_buffers                  4 16k;
    gzip_proxied                  any;
    gzip_http_version             1.1;
    gzip_comp_level               3;
    gzip_types                    text/plain application/x-javascript text/css application/xml;
    gzip_vary                     on;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';

    log_format json '{"@timestamp": "$time_iso8601",'
    '"@version": "1",'
    '"client": "$remote_addr",'
    '"url": "$uri", '
    '"status": $status, '
    '"domain": "$host", '
    '"host": "$server_addr",'
    '"size":"$body_bytes_sent", '
    '"response_time": $request_time, '
    '"referer": "$http_referer", '
    '"http_x_forwarded_for": "$http_x_forwarded_for", '
    '"ua": "$http_user_agent" } ';

    #access_log  /var/log/nginx/access.log  json;
    upstream owncloud {
        server 127.0.0.1:8000;
    }
    server {
        listen       80;
        server_name  ownclouds.maotai.org;
        location / {
            proxy_next_upstream error timeout invalid_header http_500 http_503 http_404 http_502 http_504;
            proxy_pass http://owncloud;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }

    upstream gogs {
        server 127.0.0.1:53000;
    }
    server {
        listen       80;
        server_name  gogs.maotai.org;
        location / {
            proxy_next_upstream error timeout invalid_header http_500 http_503 http_404 http_502 http_504;
            proxy_pass http://gogs;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
    server {
        listen 80;
        server_name 192.168.100.60;
        location /ngx_status {
            stub_status on;
            access_log off;
            allow all;
        }
    }
}
nginx精简版配置-包含
worker_processes auto;
worker_rlimit_nofile 65535;
error_log stderr notice;
error_log /var/log/nginx/error.log;

events {
    multi_accept on;
    use epoll;
    worker_connections 51200;
}

http {
    include                       mime.types;
    default_type                  application/octet-stream;
    server_name_in_redirect       off;
    client_max_body_size          20m;
    client_header_buffer_size     16k;
    large_client_header_buffers 4 16k;
    sendfile                      on;
    tcp_nopush                    on;
    keepalive_timeout             65;
    server_tokens                 off;
    gzip                          on;
    gzip_min_length               1k;
    gzip_buffers                  4 16k;
    gzip_proxied                  any;
    gzip_http_version             1.1;
    gzip_comp_level               3;
    gzip_types                    text/plain application/x-javascript text/css application/xml;
    gzip_vary                     on;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';

    log_format json '{"@timestamp": "$time_iso8601",'
    '"@version": "1",'
    '"client": "$remote_addr",'
    '"url": "$uri", '
    '"status": $status, '
    '"domain": "$host", '
    '"host": "$server_addr",'
    '"size":"$body_bytes_sent", '
    '"response_time": $request_time, '
    '"referer": "$http_referer", '
    '"http_x_forwarded_for": "$http_x_forwarded_for", '
    '"ua": "$http_user_agent" } ';

    include /etc/nginx/conf.d/*.conf;
}
  • /etc/nginx/conf.d/www.maotai.com

    server {
      listen       80;
      server_name  localhost;
      access_log  /var/log/nginx/host.access.log  main;
      location / {
          root   /usr/share/nginx/html;
          index  index.html index.htm;
      }
    }
  • /etc/nginx/conf.d/default.conf(某些后缀拒绝访问)

    server {
      listen       80;
      server_name  localhost;
    
      #charset koi8-r;
      #access_log  /var/log/nginx/host.access.log  main;
    
      location / {
          root   /usr/share/nginx/html;
          index  index.html index.htm;
      }
    
      #error_page  404              /404.html;
    
      # redirect server error pages to the static page /50x.html
      #
    
      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
          root   /usr/share/nginx/html;
      }
    
      # proxy the PHP scripts to Apache listening on 127.0.0.1:80
      #
      #location ~ \.php$ {
      #    proxy_pass   http://127.0.0.1;
      #}
    
      # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
      #
      #location ~ \.php$ {
      #    root           html;
      #    fastcgi_pass   127.0.0.1:9000;
      #    fastcgi_index  index.php;
      #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
      #    include        fastcgi_params;
      #}
    
      # deny access to .htaccess files, if Apache's document root
      # concurs with nginx's one
      #
      #location ~ /\.ht {
      #    deny  all;
      #}
    }
  • /etc/nginx/conf.d/nginx-status.conf

    
    #==================================================
    
    
    #                nginx status start
    
    
    #==================================================
    
      #yum install -y httpd-tools
      ## 首次创建密码文件
      #htpasswd -cmb /usr/local/nginx/conf/.pass admin 123456
      #
    
    ## 添加用户
    
      #htpasswd .pass guest
      #
    
    ## 改密码
    
      #htpasswd .pass fdipzone
    
      ## 删除用户
      #htpasswd -D .pass guest
      server {
          listen 80;
          server_name 192.168.100.60;
          auth_basic "secret";
          auth_basic_user_file /etc/nginx/conf/.pass;
    
          location /ngx_status {
              stub_status on;
              allow all;
              access_log off;
              allow 127.0.0.1;
              allow 192.168.1.0/24;
              allow 192.168.100.0/24;
              deny all;
          }
    
          #第三方状态模块: https://github.com/vozlt/nginx-module-vts
          location /ngx_statuss {
              vhost_traffic_status_display;
              vhost_traffic_status_display_format html;
              access_log off;
              allow 127.0.0.1;
              allow 192.168.1.0/24;
              allow 192.168.100.0/24;
              deny all;
          }
      }
nginx配置详细版-无include
worker_processes auto;
worker_rlimit_nofile 65535;
error_log stderr notice;
error_log /var/log/nginx/error.log;

events {
    multi_accept on;
    use epoll;
    worker_connections 51200;
}

http {
    include                       mime.types;
    default_type                  application/octet-stream;
    server_name_in_redirect       off;
    client_max_body_size          20m;
    client_header_buffer_size     16k;
    large_client_header_buffers 4 16k;
    sendfile                      on;
    tcp_nopush                    on;
    keepalive_timeout             65;
    server_tokens                 off;
    gzip                          on;
    gzip_min_length               1k;
    gzip_buffers                  4 16k;
    gzip_proxied                  any;
    gzip_http_version             1.1;
    gzip_comp_level               3;
    gzip_types                    text/plain application/x-javascript text/css application/xml;
    gzip_vary                     on;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';

    log_format json '{"@timestamp": "$time_iso8601",'
    '"@version": "1",'
    '"client": "$remote_addr",'
    '"url": "$uri", '
    '"status": $status, '
    '"domain": "$host", '
    '"host": "$server_addr",'
    '"size":"$body_bytes_sent", '
    '"response_time": $request_time, '
    '"referer": "$http_referer", '
    '"http_x_forwarded_for": "$http_x_forwarded_for", '
    '"ua": "$http_user_agent" } ';

    #access_log  /var/log/nginx/access.log  json;
    upstream owncloud {
        server 127.0.0.1:8000;
    }
    server {
        listen       80;
        server_name  ownclouds.maotai.org;
        location / {
            proxy_next_upstream error timeout invalid_header http_500 http_503 http_404 http_502 http_504;
            proxy_pass http://owncloud;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }


    upstream gogs {
        server 127.0.0.1:53000;
    }
    server {
        listen       80;
        server_name  gogs.maotai.org;
        location / {
            proxy_next_upstream error timeout invalid_header http_500 http_503 http_404 http_502 http_504;
            proxy_pass http://gogs;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
    #==================================================
    #                nginx status start
    #==================================================
    #yum install -y httpd-tools
    ## 首次创建密码文件
    #htpasswd -cmb /usr/local/nginx/conf/.pass admin 123456
    #
## 添加用户
    #htpasswd .pass guest
    #
## 改密码
    #htpasswd .pass fdipzone

    ## 删除用户
    #htpasswd -D .pass guest
    server {
        listen 80;
        server_name 192.168.100.60;
        #        auth_basic "secret";
        #        auth_basic_user_file /etc/nginx/conf/.pass;

        location /ngx_status {
            stub_status on;
            allow all;
            access_log off;
            #            allow 127.0.0.1;
            #            allow 192.168.1.0/24;
            #            allow 192.168.100.0/24;
            #            deny all;
        }

        #--------- https://github.com/vozlt/nginx-module-vts
        #        location /ngx_statuss {
        #            vhost_traffic_status_display;
        #            vhost_traffic_status_display_format html;
        #            access_log off;
        #            allow 127.0.0.1;
        #            allow 192.168.1.0/24;
        #            allow 192.168.100.0/24;
        #            deny all;
        #        }

    }
    #==================================================
    #                nginx status stop
    #==================================================
}
nginx tcp端口映射
error_log stderr notice;

worker_processes auto;
events {
  multi_accept on;
  use epoll;
  worker_connections 1024;
}

stream {
        upstream kube_apiserver {
            least_conn;
            server 192.168.8.161:6443;
            server 192.168.8.162:6443;
            server 192.168.8.163:6443;
                    }

        server {
            listen        127.0.0.1:6443;
            proxy_pass    kube_apiserver;
            proxy_timeout 10m;
            proxy_connect_timeout 1s;

        }
}

nginx列出目录和认证

 error_log /usr/local/nginx/logs/error.log;

    server {
        listen 80;
        server_name 192.168.60.123;

        location / {
            root /server/soft_pkgs;
            autoindex on;
            autoindex_localtime on; #之类的参数写这里
            autoindex_exact_size off;
        }
        location /ngx_status {
            auth_basic "secret";
            auth_basic_user_file /usr/local/nginx/conf/.pass;
            stub_status on;
            # access_log  off;
            allow 127.0.0.1;
            allow 192.168.10.0/24;
            allow 192.168.60.0/24;
            deny all;
        }
        location /ngx_statuss {
            auth_basic "secret";
            auth_basic_user_file /usr/local/nginx/conf/.pass;
            check_status;
            # access_log off;
            #allow IP;
            #deny all;
        }
    }

htpasswd使用

# yum install -y httpd-tools
## 首次创建密码文件
    #htpasswd -cmb /usr/local/nginx/conf/.pass admin 123456
    #
## 添加用户
    #htpasswd .pass guest
    #
## 改密码
    #htpasswd /usr/local/nginx/conf/.pass  admin

## 删除用户
    #htpasswd -D .pass guest

nginx_upstream_check_module


参考:
http://nolinux.blog.51cto.com/4824967/1594029

    upstream nexus {
        server 192.168.66.222:8081;
        check interval=60000 rise=2 fall=5 timeout=1000 type=tcp; #仅需要添加这行即可
    }


    server {
        listen 80;
        server_name 192.168.60.123;
        auth_basic "secret";
        auth_basic_user_file /usr/local/nginx/conf/.pass;
        location /ngx_status {
            stub_status on;
            # access_log  off;
            allow 127.0.0.1;
            allow 192.168.10.0/24;
            allow 192.168.60.0/24;
            deny all;
        }
        location /ngx_statuss {
            check_status;
            # access_log off;
            #allow IP;
            #deny all;
        }
    }

nginx-module-vts


参考:
https://github.com/vozlt/nginx-module-vts#installation

https://github.com/kubernetes/ingress-nginx

mkdir /usr/local/nginx/3rdmodules
cd /usr/local/nginx/3rdmodules
git clone git://github.com/vozlt/nginx-module-vts.git

./configure --user=nginx --group=nginx --prefix=/usr/local/tengine-2.1.2 --with-http_stub_status_module --with-http_ssl_module --add-module=/usr/local/nginx/3rdmodules/nginx-module-vts
make && make install

    server {
        listen 80;
        server_name 192.168.6.123;
        auth_basic "secret";
        auth_basic_user_file /usr/local/nginx/conf/.pass;

        location /ngx_status {
            stub_status on;
            allow 127.0.0.1;
            allow 192.168.8.0/24;
            allow 192.168.9.0/24;
            deny all;
        }
        location /ngx_statuss {
            stub_status on;
            vhost_traffic_status_display;
            vhost_traffic_status_display_format html;
            allow 127.0.0.1;
            allow 192.168.8.0/24;
            allow 192.168.9.0/24;
            deny all;
        }
    }
nginx补丁

http://jweiang.blog.51cto.com/8059417/1433675

vhost_traffic_status第三方nginx状态模块

nginx默认配置
[root@node1 ~]# docker run -it --rm nginx bash
root@ecbb058e6434:/# cat /etc/nginx/nginx.conf 

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
root@ecbb058e6434:/# cat /etc/nginx/conf.d/default.conf 
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

<think>好的,我现在需要处理用户关于安装和配置PHP-FPM与Nginx的查询。首先,用户希望得到教程或指南,所以应该分步骤说明安装和配置过程,同时确保引用提供的三个参考资料中的信息。 首先,我需要回顾用户提供的三个引用内容。引用[1]提到了克隆一个GitHub项目,修改Nginx配置并启动服务。引用[2]讨论了php.ini和php-fpm.conf的配置文件。引用[3]涉及使用systemctl启动Nginx和PHP-FPM服务,以及配置文件的介绍。这些信息需要整合到回答中。 接下来,用户的问题是关于安装和配置的教程,所以步骤应该包括安装、配置、启动服务以及验证。需要检查每个步骤是否在引用中有支持,比如安装命令、配置文件的位置、启动服务的命令等。 可能需要注意不同操作系统的差异,比如Ubuntu和CentOS的包管理器不同,但用户提供的引用中没有明确提到系统类型,所以可能需要保持一般性,或者假设使用常见的系统如Ubuntu。例如,安装Nginx和PHP-FPM可能使用apt-get或yum,但引用中没有具体说明,可能需要用通用命令,或参考引用中的信息。 引用[1]提到克隆项目到本地,并进入目录修改配置,这部分可能需要详细说明,比如用户是否需要使用这个特定的GitHub项目,或者是否需要手动配置。需要判断用户是否需要从零开始配置还是使用现有项目。可能两种方法都要提到,但根据引用中的信息,可能需要建议用户克隆项目作为示例,但也要说明手动配置的步骤。 配置部分,Nginx配置文件需要指向PHP-FPM的socket或TCP端口。引用[2]提到php.ini和php-fpm.conf的配置,可能需要调整这些文件中的设置,比如listen = 127.0.0.1:9000或者使用Unix socket。引用[3]提到Nginx配置文件的位置,可能需要指导用户修改server块,添加处理PHP脚本的部分。 启动服务方面,引用[3]提到了使用systemctl启动nginx和php-fpm,所以需要包括这些命令。同时,可能需要提醒用户启用服务以便开机启动,比如systemctl enable。 验证部分,可能需要创建一个phpinfo文件并通过浏览器访问,确保Nginx正确处理PHP请求。此外,权限问题也是常见的,比如PHP-FPM的用户和组是否与Nginx一致,可能需要检查www-data用户或类似。 最后,生成相关问题需要考虑用户可能的后续疑问,比如性能优化、常见错误处理、多站点配置等,基于安装和配置的步骤,提出相关的问题。 现在需要将上述思考整理成结构化的步骤,确保每个部分都有引用支持,并正确标注引用来源。同时,注意使用中文,行内公式正确,独立公式单独成段,但当前问题不涉及数学公式,所以可能不需要处理这部分。但需要遵守用户给出的系统级指令,比如使用正确的LaTeX格式,不过在这个问题中可能用不上,但需要确保其他格式正确,如代码块和引用标识。 总结步骤: 1. 安装Nginx和PHP-FPM:根据引用[2]和[3],使用包管理器安装,可能涉及apt或yum命令。 2. 配置PHP-FPM:编辑php.ini和php-fpm.conf(引用[2]),确保监听方式正确。 3. 配置Nginx:修改nginx.conf或sites-available中的server块,处理PHP请求(引用[1]和[3])。 4. 启动服务:使用systemctl启动并启用服务(引用[3])。 5. 验证配置:创建测试PHP文件,检查是否解析正确。 6. 权限和优化:调整用户/组,日志配置等(引用[2]可能涉及)。 需要确保每个步骤中的具体命令和文件路径正确,并正确标注引用来源。例如,启动服务的命令来自引用[3],配置文件的修改来自引用[1]和[2]。 可能需要注意用户是否需要详细代码示例,比如具体的Nginx配置块,PHP-FPM的配置参数。例如,在Nginx的server块中添加location ~ \.php$部分,指定fastcgi_pass的参数,这需要根据PHP-FPM的监听方式(如127.0.0.1:9000或unix socket)。 此外,用户提供的引用中有GitHub项目,可能需要建议用户参考该项目,如引用[1]中的项目,以便获取示例配置,但也要说明手动配置的方法。 最后,生成相关问题要考虑用户可能的后续需求,比如如何配置多个站点、HTTPS设置、性能调优、日志管理等。</think>### 分步安装与配置PHP-FPM与Nginx教程 以下是安装和配置PHP-FPM与Nginx的核心步骤: --- #### **1. 安装Nginx与PHP-FPM** - **Ubuntu/Debian系统** ```bash sudo apt update sudo apt install nginx php-fpm php-cli ``` - **CentOS/RHEL系统** ```bash sudo yum install epel-release sudo yum install nginx php-fpm php-cli ``` 安装完成后,验证PHP-FPM本: ```bash php-fpm -v ``` --- #### **2. 配置PHP-FPM** - **修改PHP配置文件** 编辑`php.ini`(路径通常为`/etc/php/<本>/fpm/php.ini`)[^2]: ```ini cgi.fix_pathinfo=0 # 提升安全性 ``` - **调整PHP-FPM监听方式** 修改`php-fpm.conf`或池配置文件(如`www.conf`)[^2],确保监听地址与Nginx匹配: ```ini listen = /run/php/php-fpm.sock # 使用Unix Socket(推荐) ; 或 listen = 127.0.0.1:9000 # 使用TCP端口 ``` 设置用户和组与Nginx一致(例如`www-data`)。 --- #### **3. 配置Nginx** - **修改Nginx虚拟主机配置** 编辑站点配置文件(如`/etc/nginx/sites-available/default`),添加PHP支持[^1][^3]: ```nginx server { listen 80; server_name example.com; root /var/www/html; location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php-fpm.sock; # 与PHP-FPM监听方式一致 # 或 fastcgi_pass 127.0.0.1:9000; } } ``` - **检查配置语法并重载服务** ```bash sudo nginx -t sudo systemctl reload nginx ``` --- #### **4. 启动与验证服务** - **启动并启用服务** ```bash sudo systemctl start nginx php-fpm sudo systemctl enable nginx php-fpm ``` - **创建测试文件** 在网站根目录(如`/var/www/html`)创建`info.php`: ```php <?php phpinfo(); ?> ``` 访问`http://服务器IP/info.php`,若显示PHP信息页面则配置成功。 --- #### **5. 权限与日志管理** - **确保文件权限正确** Nginx用户(如`www-data`)需有网站目录的读取权限: ```bash sudo chown -R www-data:www-data /var/www/html ``` - **查看日志排查问题** - Nginx错误日志:`/var/log/nginx/error.log` - PHP-FPM日志:`/var/log/php-fpm.log` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值