Home Backend Development PHP Tutorial nginx how to configure file example

nginx how to configure file example

May 22, 2018 pm 04:05 PM
nginx Example Configuration file

This article mainly shares with you how to configure nginx file sample code, I hope it can help you.

nginx.conf

user  www www; # Nginx的worker进程运行用户以及用户组worker_processes 4; # 启动进程数,通常设置成和cpu的数量相等#worker_processes auto;#以下参数指定了哪个cpu分配给哪个进程,一般来说不用特殊指定。如果一定要设的话,用0和1指定分配方式.#这样设就是给1-4个进程分配单独的核来运行,出现第5个进程是就是随机分配了。#worker_processes 4     #4核CPU #worker_cpu_affinity 0001 0010 0100 1000error_log  /home/wwwlogs/nginx_error.log  crit; # 定义全局错误日志定义类型,[debug|info|notice|warn|crit]pid        /usr/local/nginx/logs/nginx.pid; # PID文件#Specifies the value for maximum file descriptors that can be opened by this process.# 一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n的值保持一致。worker_rlimit_nofile 51200;# 工作模式及连接数上限events
    {        # use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
        use epoll; 
        worker_connections 51200; # 单个后台worker process进程的最大并发链接数
        #worker工作方式:串行(一定程度降低负载,但服务器吞吐量大时,关闭使用并行方式)
        multi_accept on;
    }

http
    {        #文件扩展名与文件类型映射表
        include       mime.types;        #默认文件类型
        default_type  application/octet-stream;
        服务器名字的hash表大小
        server_names_hash_bucket_size 128;        #指定来自客户端请求头的hearerbuffer大小
        client_header_buffer_size 32k;        #指定客户端请求中较大的消息头的缓存最大数量和大小。
        large_client_header_buffers 4 32k;        #客户端请求单个文件的最大字节数
        client_max_body_size 50m;

        sendfile   on; #开启高效传输模式。
        #防止网络阻塞
        tcp_nopush on;

        keepalive_timeout 60; # 连接超时时间 ,单位是秒

        tcp_nodelay on;        #FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;

        gzip on; # 开启gzip压缩
        gzip_min_length  1k; #最小压缩文件大小
        gzip_buffers     4 16k; #压缩缓冲区
        gzip_http_version 1.1; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
        gzip_comp_level 2; #压缩等级 1-9 等级越高,压缩效果越好,节约宽带,但CPU消耗大
        gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss; #压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
        #前端缓存服务器缓存经过压缩的页面
        gzip_vary on;
        gzip_proxied   expired no-cache no-store private auth;
        gzip_disable   "MSIE [1-6]\.";        #limit_conn_zone $binary_remote_addr zone=perip:10m;
        ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.

        server_tokens off;
        access_log off;        # 定义日志格式
        log_format up_head '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" "deviceid: $http_deviceid"'
                        '"$request_body"  $upstream_response_time "$request_time"';


    include vhost/*.conf;
}
Copy after login

vhost/test.conf

server {
    listen 80;
    listen 443 ssl;
    server_name www.test.com;

    ssl_certificate      /nginx/conf/https/www.test.com.crt;
    ssl_certificate_key  /nginx/conf/https/www.test.com.key;
    root   /www/www.test.com;
    index  index.html index.htm index.php;

    location ~^/test.php {
          default_type    application/json;          return 404 '{404}';
          access_log off;
    }

    location ~ ^/test/status(.*)$
    {
        default_type text/html;
        access_log /log/nginx/access/dot.log;        return 200 '';
    }

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    location ~\.php$ {
            # 如果用到 sock 则值参考 unix:/var/run/php/php7.0-fpm.sock
            fastcgi_pass             unix:/tmp/php-cgi.sock;
            fastcgi_index            index.php;
            fastcgi_param            SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include                  fastcgi_params;
            access_log /log/nginx/access/access.log up_head;
     }

    #静态文件
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
    }

    location ~ .*\.(js|css)?$
    {
        expires      12h;
    }

    location ~ /.well-known {
        allow all;
    }

    location ~ /\.
    {
        deny all;
    }

    error_log /log/nginx/error/error.log;
}
Copy after login

Definition of log format parameters:

    #定义日志的格式。后面定义要输出的内容。
    #1.$remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址;
    #2.$remote_user :用来记录客户端用户名称;
    #3.$time_local :用来记录访问时间与时区;
    #4.$request  :用来记录请求的url与http协议;
    #5.$status :用来记录请求状态; 
    #6.$body_bytes_sent :记录发送给客户端文件主体内容大小;
    #7.$http_referer :用来记录从那个页面链接访问过来的;
    #8.$http_user_agent :记录客户端浏览器的相关信息
Copy after login

Related recommendations:

Detailed explanation of Nginx configuration file nginx.conf

Detailed explanation of Nginx configuration file example

Simple description of Nginx configuration file

The above is the detailed content of nginx how to configure file example. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to allow external network access to tomcat server How to allow external network access to tomcat server Apr 21, 2024 am 07:22 AM

To allow the Tomcat server to access the external network, you need to: modify the Tomcat configuration file to allow external connections. Add a firewall rule to allow access to the Tomcat server port. Create a DNS record pointing the domain name to the Tomcat server public IP. Optional: Use a reverse proxy to improve security and performance. Optional: Set up HTTPS for increased security.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Welcome to nginx!How to solve it? Welcome to nginx!How to solve it? Apr 17, 2024 am 05:12 AM

To solve the "Welcome to nginx!" error, you need to check the virtual host configuration, enable the virtual host, reload Nginx, if the virtual host configuration file cannot be found, create a default page and reload Nginx, then the error message will disappear and the website will be normal show.

How to generate URL from html file How to generate URL from html file Apr 21, 2024 pm 12:57 PM

Converting an HTML file to a URL requires a web server, which involves the following steps: Obtain a web server. Set up a web server. Upload HTML file. Create a domain name. Route the request.

Where is the win10 user profile? How to set the user profile in Win10 Where is the win10 user profile? How to set the user profile in Win10 Jun 25, 2024 pm 05:55 PM

Recently, many Win10 system users want to change the user profile, but they don’t know how to do it. This article will show you how to set the user profile in Win10 system! How to set up user profile in Win10 1. First, press the "Win+I" keys to open the settings interface, and click to enter the "System" settings. 2. Then, in the opened interface, click "About" on the left, then find and click "Advanced System Settings". 3. Then, in the pop-up window, switch to the "" option bar and click "User Configuration" below.

How to deploy nodejs project to server How to deploy nodejs project to server Apr 21, 2024 am 04:40 AM

Server deployment steps for a Node.js project: Prepare the deployment environment: obtain server access, install Node.js, set up a Git repository. Build the application: Use npm run build to generate deployable code and dependencies. Upload code to the server: via Git or File Transfer Protocol. Install dependencies: SSH into the server and use npm install to install application dependencies. Start the application: Use a command such as node index.js to start the application, or use a process manager such as pm2. Configure a reverse proxy (optional): Use a reverse proxy such as Nginx or Apache to route traffic to your application

What are the most common instructions in a dockerfile What are the most common instructions in a dockerfile Apr 07, 2024 pm 07:21 PM

The most commonly used instructions in Dockerfile are: FROM: Create a new image or derive a new image RUN: Execute commands (install software, configure the system) COPY: Copy local files to the image ADD: Similar to COPY, it can automatically decompress tar archives or obtain URL files CMD: Specify the command when the container starts EXPOSE: Declare the container listening port (but not public) ENV: Set the environment variable VOLUME: Mount the host directory or anonymous volume WORKDIR: Set the working directory in the container ENTRYPOINT: Specify what to execute when the container starts Executable file (similar to CMD, but cannot be overwritten)

Can nodejs be accessed from the outside? Can nodejs be accessed from the outside? Apr 21, 2024 am 04:43 AM

Yes, Node.js can be accessed from the outside. You can use the following methods: Use Cloud Functions to deploy the function and make it publicly accessible. Use the Express framework to create routes and define endpoints. Use Nginx to reverse proxy requests to Node.js applications. Use Docker containers to run Node.js applications and expose them through port mapping.

See all articles