Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Basic configuration schemes commonly used in Nginx

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "the basic configuration scheme commonly used in Nginx". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the basic configuration scheme commonly used in Nginx.

Parameter setting of fastcgi module for Nginx

Nginx has two configuration files, fastcgi_params and fastcgi.conf. The only difference between the two is that one more parameter SCRIPT_FILENAME,diff for fastcgi.conf is shown as follows:

$diff fastcgi fastcgi_params < fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;vim enter / usr/local/nginx/conf/fastcgi_params file # parameters of the request, such as? app=123fastcgi_paramfastcgi_param QUERY_STRING $query_string;## request action (GET,POST) fastcgi_param REQUEST_METHOD $request_method;# request header Content-Type field fastcgi_param CONTENT_TYPE $content_type;# request header Content-length field fastcgi_param CONTENT_LENGTH $content_length # script name fastcgi_param SCRIPT_NAME $fastcgi_script_name;# requested address without parameters fastcgi_param REQUEST_URI $request_uri;# is the same as $uri the root directory of the fastcgi_param DOCUMENT_URI $document_uri;# website. The protocol specified in the root directive in the server configuration fastcgi_param DOCUMENT_ROOT $document_root;# request, usually HTTP/1.0 or HTTP/1.1fastcgi_param SERVER_PROTOCOL $server_protocol;#https if the value is not empty, set the fastcgi_param HTTPS $https if_not_empty;#cgi version fastcgi_param GATEWAY_INTERFACE CGI/1.1;#nginx version number, which can be modified or hidden fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;# client IPfastcgi_param REMOTE_ADDR $remote_addr # client port Fastcgi _ param REMOTE_PORT $remote_port;# server IP address fastcgi_param SERVER_ADDR $server_addr;# server port fastcgi_param SERVER_PORT $server_port;# server name, the domain name server_namefastcgi_param SERVER_NAME $server_name; customizable variable fastcgi_param PATH_INFO $path_info;# specified in the server configuration can be appended with another line at the end to save the same fastcgi_param REDIRECT_STATUS 200as fastcgi.conf Fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; can print out the above service environment variables in php, such as: echo $_ SERVER ['REMOTE_ADDR']

Common instruction interpretation of Nginx

The fastcgi_pass command specifies which fastcgi server interface to proxy http to. The fastcgi_pass is followed by a fastcgi server address, which can be a domain address, a Uninx- domain socket, or a reverse proxy set in upstream. Fastcgi_pass localhost:9000; # default PHP starts at port 9000 fastcgi_pass unix:/tmp/fastcgi.socket;fastcgi_pass upstream_php5; # the reverse proxy specified here can set fastcgi_param in nginx.conf. This command sets the parameters in the fastcgi request. The default parameters are in the fastcgi module parameter file mentioned above, and the specific settings can be obtained in $_ SERVER. For example, if you want to set the current machine environment, you can use fastcgi_param ENV test; to set it. For php, the minimum variables you need to set are: the command fastcgi_param SCRIPT_FILENAME / home/www/scripts/php$fastcgi_script_name;fastcgi_param QUERY_STRING $query_string;fastcgi_index sets the script that fastcgi uses by default. That is, when SCRIPT_FILENAME misses the script, the script set by fastcgi_index is used. Fastcgi_index index.php

The above three commands make up the most basic fastcgi settings:

Location / {the following fastcgi_pass localhost:9000;fastcgi_index index.php;# can specify fastcgi_param SCRIPT_FILENAME / home/www/scripts/php$fastcgi_script_name;fastcgi_param QUERY_STRING $query_string;fastcgi_param REQUEST_METHOD $request_method;fastcgi_param CONTENT_TYPE $content_type;fastcgi_param CONTENT_LENGTH $content_length;} directly in the fastcgi_param configuration file.

Picture (or static file) server configuration

Server {listen 80 images.xxx.com img.movie.xxx.com;root / data/vhosts/xxx.com/images/public_html/;index index.shtml index.html index.htm;# if it is a js, css, json file, you can specify compression to reduce the transfer file size gzip_types text/plain application/x-javascript text/css application/xml text/xml application/json;}

Basic server

Server {listen 80 serverSecretname www.xxx.com;root / data/vhosts/xxxx.com/public_html/;index index.htm index.php index.html index.shtml;location / {ssi on;ssi_silent_errors on;ssi_types text/shtml;include other.conf; # here you can configure other public configurations, or rewrite rules} location ~\ .php ${expires off;include fastcgi_params; # fastcgi specified parameters to configure fastcgi_pass 127.0.0.1 data/vhosts/xxxx.com/public_html/;index index.htm index.php index.html index.shtml;location 9000 # the agent or socketfastcgi_index index.php;fastcgi_connect_timeout 300 can also be specified here as above.

The directory to which ssi_inclue access is configured does not exist that is the specified directory

Location ~ / ssi_include/ {if (!-e $request_filename) {rewrite ^ (. *) $/ blank.html last;}}

Configure the default error page for static files

Location ~ (\ .html |\ .htm |\ .shtml) ${error_page 404500 502 503 504 / 404.html;}}

Auth permission Settings

Step 1. Under the root domain name, you need to configure the permissions of the directory to set location.

Location / phpMyAdmin/ {allow 192.168.0.1 * allow xx.xx.xxx.xxx;allow xx.xx.xxx.xxx;deny all;auth_basic "Restricted"; auth_basic_user_file / usr/local/nginx/conf/auth_phpmyadmin.pass;}

Step2. Add the account password to the file specified by auth_basic_user_file, one per line

Username1:password1username2:password2username3:password3username4:password4

Nginx reverse proxy

The first reverse proxy:

Location / {proxy_pass http://192.168.1.4:8099/;# if acting for different directories, put the following configuration on top of the root directory agent # proxy_pass http://192.168.1.4:8099/linuxtone/;proxy_redirect default;}

The second reverse proxy:

Upstream configures upstream xx.xxx.com {server 192.168.1.4 server 8099;}

Site profile

Server {listen 80th serverroomname bbs.linuxtone.conf;index index.html index.htm;root / date/vhosts/xxx.com/;location ~ ^ / NginxStatus/ {stub_status on;access_log off;} location / {proxy_redirect off; proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header REMOTE-HOST $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;client_max_body_size 50m # buffer proxy buffers the maximum number of bytes requested by the client, which can be understood as saving it locally and then passing it to the user client_body_buffer_size 256k proxy connecttimeout 30; # nginx and backend server connection timeout (proxy connection timeout) proxy_send_timeout 30th proxy readout timeout 60; # response time of back-end server (proxy receiving timeout) proxy_buffer_size 256k after a successful connection # set the buffer size of proxy server (nginx) to hold account information proxy _ buffers 4 256k; # proxy_buffers buffer, if the average size of a web page is less than 256k, set proxy_busy_buffers_size 256k. This sets the size of proxy_busy_buffers_size 256k. # Agent is not allowed to actively close the connection # http://xx.xxx.com refers to the name of the upstream block above proxy_pass http://xx.xxx.com;}

Apache reverse proxy

# set the domain name to be forwarded to port 8080

ServerAdmin webmaster@dummy-host2.example.comServerName www.xxx.comProxyRequests offOrder deny,allowAllow from allProxyPass / http://www.xxx.com:8080/ProxyPassReverse / http://www.xxx.com:8080/

ProxyPassReverse is generally used in conjunction with the ProxyPass instruction, which causes Apache to adjust the URL in the Location, Content-Location, and URI headers of the HTTP redirect reply, which avoids the use of Apache as a reverse proxy. The problem of bypassing reverse proxy caused by HTTP redirection of back-end server

No access by spiders

# judge UA. If UA does not contain spider or bot (case-insensitive), UA is a normal user

# set the variable is_ value to yes

If ($http_user_agent! ~ * "spider | bot") {set $is_human 'yes';}

# when there is any request and the UA is not a normal user, it means it should be a spider program, and 403 is returned

Location / {if ($is_human! = 'yes') {return 403;}}

# when there is any request

Location / {# when the visitor UA contains spider or bot (case-insensitive), it means that the if is visited by spiders ($http_user_agent ~ * "spider | bot") {# directly visit return 403 from the whole site of the shielded spider;}}

A robots.txt file has been added to the system:

User-agent: * Disallow: / at this point, I believe you have a better understanding of the "basic configuration scheme commonly used in Nginx". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report