Detailed code of Nginx configuration

不言
Release: 2023-04-03 10:14:01
Original
1569 people have browsed it

This article shares with you the detailed code about Nginx configuration. The content is very good. Friends in need can refer to it. I hope it can help everyone.

The previous blog post has already talked about the method of building LNMP environment. After installation, you first need to understand the nginx configuration file: /usr/local/nginx/conf/nginx.conf, I Remove all the comments in the configuration file and those that are temporarily unused, so that it looks cleaner:

// 全局区
worker_processes 1; // 有1个工作的子进程,会占用CPU,可自由设置,一般设置为:CPU数*核数,如果想查看工作中的进程,可以使用命令:ps aux|grep nginx

Event { // 一般是配置nginx连接的特性
   worker_connections 1024; // 这是指一个worker能同时允许多少连接

} 

http { //这是配置http服务器的主要段

 #日志管理默认为main格式,记录的内容为: 远程IP:$remote_addr | 用户时间:$remote_user [$time_local] | 请求方法(如GET/POST):$request | 请求状态:$status | 请求体body长度:$body_bytes_sent | referer来源信息:$http_referer | 用户代理/蜘蛛$http-user-agent | 被转发的请求的原始IP:$http_x_forwarded_for()
   log_format main '$remote_addr - $remote_user [$time_local] "$request" ' #默认的日志配置
             '$status $body_bytes_sent "$http_referer" '
                   '"$http_user_agent" "$http_x_forwarded_for"';

    server { //这里整个server的意思就是当你在浏览器中请求127.0.0.1这个地址时,location匹配到后定位到/usr/local/nginx/html/index.html

        listen 80;  #监听端口
        server_name 127.0.0.1; #监听域名
     access_log  logs/host.access.log  main; #开启日志

        location / {//定位,把特殊的路径或文件再次定位
        root html;  #根目录定位,可以使用相对路径,此处所说的根目录为/usr/local/nginx目录,html也是相对于/usr/local/nginx目录,也可使用绝对路径定位,比如你的项目在/var/www/html/目录下,那你就可以改为root /var/www/html/
            index  index.html index.htm;  
        }

     location ~ \.php$ {//nginx转发PHP请求,碰到.php文件,把根目录定位到html,把请求转交给9000端口PHP进程, 并告诉PHP进程当前的请求的脚本是/scripts$fastcgi_script_name
       root           html;
       fastcgi_pass   127.0.0.1:9000; #默认PHP9000端口
       fastcgi_index  index.php;
       fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
       include        fastcgi_params;
     }

    }
}
Copy after login

Related recommendations:

LNMP environment How to build

Composer extension development and laravel framework application

The above is the detailed content of Detailed code of Nginx configuration. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template