Install and configure Nginx under CentOS 7 to support pseudo static (including all codes)

Install and configure Nginx under CentOS 7 to support pseudo static

It is a difficult problem for novices to install and configure Nginx under Linux to support pseudo-static state. They often encounter 404 and other errors. Configuration and debugging is a waste of time. Sometimes they feel that they can't start. Online materials usually focus on some key and difficult things, which gives people the feeling that only trees can't see the forest, and they can't see the whole picture. This blog gives a CentOS 7 installation and configuration of Nginx branch The pseudo static example is provided for your reference.

The content of the blog includes two parts:

  • One click installation
  • Configure Nginx

Content:

  • One click installation

    Use lnmp to install the package. The command is as follows:

wget http://soft.vpser.net/lnmp/lnmp1.5.tar.gz -cO lnmp1.5.tar.gz && tar zxf lnmp1.5.tar.gz && cd lnmp1.5 && ./install.sh lnmp
For details, please refer to [https://lnmp.org/install.html]
  • Configure Nginx

/The configuration contents of usr/local/nginx/conf/nginx.conf are as follows. See the code Notes for details

user  www www; #user

worker_processes auto;

error_log  /home/wwwlogs/nginx_error.log crit; #Error log output

pid        /usr/local/nginx/logs/nginx.pid; #Corresponding process

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200; 

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

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

        server_names_hash_bucket_size 128;
        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 parameter setting
        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 support
        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
        gzip_vary on;
        gzip_proxied   expired no-cache no-store private auth;
        gzip_disable   "MSIE [1-6]\.";

        server_tokens off;
        access_log off;
		
		#Site parameter configuration
        include vhost/default.conf;
}

/usr/local/nignx/conf/vhost/default.conf

server
{
        listen       80;
        server_name  localhost;
        index        index.html index.htm index.php;
        root /home/www;
        autoindex off;


        #forbid spider to draw the resources
        location ~* ^.+\.(jpg|jpeg|gif|png|swf|rar|zip|css|js|sql|html|htm|log|txt|doc|pdf|bak)$ {
                valid_referers none blocked localhost *.baidu.com *.haosou.com *.google.com *.google.hk *.sogou.com *.etao.com *.chinaz.com;
                if ($invalid_referer) {
                        return 403;
                        break;
                }

                access_log off;
                expires 30d;
         }

		#Prevent downloading various resource files
        location ~* ^.+\.(jpg|jpeg|gif|png|rar|zip|css|js|sql|html|htm|log|txt|doc|bak)$ {
                if (-f $request_filename) {
                        root /statics/;
                        return 403;
                }
        }

		#Rewrite the url to index.php mode, such as http://localhost/admin/logout - > http://localhost/index.php/admin/logout
        location / {
                index  index.html index.htm index.php;
                rewrite ^/$ /index.php last;
                rewrite ^/(?!index\.php|robots\.txt|images|js|styles|statics|install)(.*)$ /index.php/$1 last;
        }

        location ~ ^/favicon\.ico$ {
                root    /home/www;
        }

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

		#fastcgi configuration supports pathinfo and pseudo static
        include enable-php.conf;
}

/usr/local/nignx/conf/enable-php.conf

        location ~ [^/]\.php(/|$)
        {
        		#Set $fastcgi ﹐ script ﹐ name and $fastcgi ﹐ path ﹐ info
                fastcgi_split_path_info ^(.+?\.php)(/.*)$;

                if (!-f $document_root$fastcgi_script_name) {
                        return 404;
                }


                # Mitigate https://httpoxy.org/ vulnerabilities
                fastcgi_param HTTP_PROXY "";

                fastcgi_pass  unix:/tmp/php-cgi.sock;
                fastcgi_index index.php;

                include fastcgi_params;
        }


All parameters are configured in one file as follows:
/usr/local/nignx/conf/fastcgi_params

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

#Note in particular that the following four parameters script filename and path info may not exist in the default file and need to be added
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  PATH_INFO          $fastcgi_path_info;
#fastcgi_param  PATH_TRANSLATED    $document_root$fastcgi_path_info;

fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;
fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/:/tmp/:/proc/";

Reference article:
nginx official article
https://blog.jjonline.cn/linux/218.html
https://blog.csdn.net/m0_37355951/article/details/78333723

Keywords: PHP Nginx Javascript Nignx

Added by fragger on Sun, 22 Dec 2019 17:31:22 +0200