Nginx Study Notes 1

Source: Internet
Author: User

Next, let's start learning the nginx configuration file.

Then CD to the ETC/nginx directory

Part 1 nginx. conf

The main configuration file is nginx. conf.

Php-related fastcgi_params

Python-related uwsgi_params

Nginx Note 1: Well, first make sure that Apache is stopped first, because Apache uses port 80 by default, and both of them will be grabbed!

You can start nginx. conf first.

# User and group user www-data; # specify the number of work derivative processes (the default is the number of CPU threads, Which is n2600, dual-core four threads !) Worker_processes 4; # specify the path where the PID is stored. The process ID of the nginx daemon should be recorded. It is the parent process ID of other processes! (For example, pidfile in Apache) PID/var/run/nginx. PID; events {use epoll; # The best event mode in Linux! This option is available by default! # Allowed connections worker_connections 768; # multi_accept on;} HTTP {## Basic settings # sendfile on; tcp_nopush on; tcp_nodelay on; # keepalive_timeout test the time between multiple requests for transmission, and then a persistent link. If the client's two HTTP requests exceed the keepalive_timeout time, close the link to release resources !! (Also available in Apache) keepalive_timeout 65; types_hash_max_size 2048; # server_tokens off; # slave 64; # server_name_in_redirect off; Include/etc/nginx/mime. types; # What is mime? We seem to have mentioned it in our computer network, but we have to understand it! # Default_type application/octet-stream; # When the file requested by the user has a MIME type ing defined on the server, use defaulttype # What can be found? Text/HTML text/plain (a file with a suffix of txt is displayed in text) image/GIF # What is application/octet-stream here? # If the unconfirmed files are mostly software or images, we recommend that you use application/octet-stream # the browser will prompt you to save the files as "Save! ### Logging settings ### log. For details, see access_log/var/log/nginx/access. log; error_log/var/log/nginx/error. log ### gzip settings ## enable gzip compression gzip on; gzip_disable "msie6"; # gzip_vary on; # gzip_proxied any; # gzip_comp_level 6; # gzip_buffers 16 8 K; # gzip_http_version 1.1; # gzip_types text/plain text/CSS application/JSON application/X-JavaScript text/XML application/XML + RSS text/JavaScript; ### Vir Tual host configs # include/etc/nginx/CONF. d /*. conf; Include/etc/nginx/sites-enabled/*; # Very good. We can clearly see that conf will be loaded. all files in the d directory and the sites-enabled directory also prove that we can write configuration files in these two directories !} # Nginx can also be used as an email proxy server! # Mail {## see sample authentication script at: # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript## # auth_http localhost/auth. PHP ## pop3_capabilities "TOP" "user" ;## imap_capabilities "imap4rev1" "uidplus" ;## server {# listen localhost: 110; # protocol POP3; # proxy on; #}## server {# listen localhost: 143; # protocol IMAP; # proxy on ;#}#}

Part 2 nginx Virtual Host Configuration

What is a virtual host? In other words, the task I just made was to configure a virtual host. It was amazing! Haha! First, the virtual host is to divide a host into a "virtual" host. You think I have over 20 sites. We are not a big one, and we have over 20 servers, so we use the virtual host, this problem can be solved!

Such as the simplest Virtual Host Configuration File

Similar to Apache, nginx can also be configured with multiple types of virtual hosts.

  • Domain name-based VM
  • IP-based VM
  • Port-based VM

Then we found that there are two directories, site-available and site-enabled, under the same directory as Apache. We generally use the method to write the configuration file under the former, make a soft connection in the latter directory! The reason is like the directory name. The former is an existing website, and the latter is a directory in use! By default, nginx loads the site-enabled directory! The former directory has a default to show us how to write the configuration file of the VM.

Let's take a look at this section:

# You may add here your # server {#... #}# statements for each of your virtual hosts to this file # This text clearly shows us how to add a VM! ### You shoshould look at the following URL's in order to grasp a solid understanding # Of nginx configuration files in order to fully unleash the power of nginx. # http://wiki.nginx.org/Pitfalls# http://wiki.nginx.org/QuickStart# http://wiki.nginx.org/Configuration## generally, you will want to move this file somewhere, and start with a clean # file but keep this around for reference. or just disable in Sites-enabled. # Please see/usr/share/doc/nginx-DOC/examples/for more detailed examples. #### For more information, see those websites and examples! Server {# Starts directly starting the server header. If the configuration is in nginx. conf, write it in HTTP {...... Server {......}} Is based on the HTTP protocol! # Listen 80; # Listen for IPv4; this line is default and implied # Listen to port 80! # Listen [:]: 80 default ipv6only = on; # Listen for IPv6 # Root HTML webpage file storage directory root/usr/share/nginx/WWW; # default homepage file, from left to right! If index.html is not found, go to index.htm 'index index.html index.htm; # Make site accessible from http: // localhost/SERVER_NAME localhost; # host name location/{# first attempt to serve request as file, then # As directory, then fall back to index.html try_files $ URI // index.html;} # You can directly go to the home page by accessing the root directory! Location/DOC {root/usr/share; Autoindex on; allow 127.0.0.1; deny all;} location/images {root/usr/share; Autoindex off;} #404, 50x page and its address # error_page 404/404 .html; # redirect server error pages to the static page/50x.html # error_page 500 502 503 x.html; # location =/50x.html {# root/usr/share/nginx/WWW; #}# 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 $ {# fastcgi_pass 127.0.0.1: 9000; # fastcgi_index index. PHP; # include fastcgi_params; #}# deny access. htaccess files, if Apache's document root # concurs with nginx's one # location ~ /\. HT {# deny all ;#}# another virtual host using mix of IP-, name-, and port-based configuration # server {# listen 8000; # Listen somename: 8080; # SERVER_NAME somename alias another. alias; # Root HTML; # index index.html index.htm; ## location/{# try_files $ URI // index.html; #}#}# https server ## server {# listen 443; # SERVER_NAME localhost; ## root HTML; # index index.html index.htm; ## SSL on; # SS L_certificate cert. pem; # ssl_certificate_key cert. Key; # ssl_session_timeout 5 m; # ssl_protocols SSLv3 tlsv1; # ssl_ciphers all :! ADH :! Export56: RC4 + RSA: + high: + medium: + low: + SSLv3: + exp; # ssl_prefer_server_ciphers on; ## location/{# try_files $ URI // index.html; #}#}

We can find that the basic format for configuring multiple virtual hosts is

Server {......}

Server {......}

Then let's take a look at the nginx log.

Part 3 nginx logs and nginx log-related commands mainly include two

Log_format # Set the log format

Access_log # specify the storage path, format, and cache size of the log file

The two commands can be placed in HTTP {......} Or server {......} Between

Log_format xxxx (the log name cannot be repeated !) '$ Remote_addr [$ time_local] ''" $ request "$ Status $ body_bytes_sent'' "$ http_referer" "$ http_user_agent" ''$ http_x_forwarded_for '; $ remote_addr # reverse proxy server IP address $ time_local # used to record the access time and time zone $ request # used to record the request URL and HTTP Protocol $ status # used to record the Request status $ body_bytes_sent # sent to the client file topic content size $ http_referer # record the $ http_user_agent accessed from the page Link # used to record information about the client browser $ http_x_forwarded_for # record the Client IP address and the server address requested by the client (this is because it is a reverse proxy server, the IP address that the Web server cannot provide.

Access_log

Access_log path [format {buffer = size | off}]

# Do not use logs

Access_log off

# Use logs in the default XXXX format

Access_log/data/logs/file. Log xxxx

 

 

Next, let's take a practical example. This is a virtual host configuration file under nginx on the testing server of Jinghong network!
# Servername bbs.zjut.com # serveralias v.zjut.com zookeeper # DocumentRoot/opt/www/BBS # Jump to bbs.zjut.com # server {Listen 80; # SERVER_NAME v.zjut.com; # rewrite ^ /(. *) $ http://bbs.zjut.com/#1 permanent; # access_log off; #}# jump to BBS. zjut. inserver {Listen 80; # Listen to port 80 SERVER_NAME bbs2.zjut. in; # host name rewrite ^ /(. *) $ http://bbs.zjut.in/#1 permanent; access_log off; # log off} # bb S. zjut. comserver {Listen 80; # Listen to port 80 SERVER_NAME BBS. zjut. in; root/opt/www/BBS; # Put the directory in/opt/www/bbsindex index.html index.htm index.php?#first find index.html, then index.htm, and then index. phplog_format bbs_log '$ remote_addr [$ time_local] ''" $ request "$ Status $ body_bytes_sent'' "$ http_referer" "$ http_user_agent" ''$ http_x_forwarded_for '; # define the log format # access_log/opt/log/nginx/BBS. log bbs_log; error_page 404 =/errorpage/40 4. html; #404 error page address error_page 500 502 503 504 =/errorpage/500.html; # 50x error address directory location ~. * \. (GIF | JPG | JPEG | PNG | BMP | SWF | ico) $ {expires 30d;} location ~. * \. (JS | CSS | ASP | aspx )? $ {Expires 1 h;} location/uploadfile {return 404;} location/uploadfile {return 404;} # location ~. * \. (SH | bash | ASP )? $ # {Return 403;} # location ~ *\. (HTML | HTM | XML | WMV | Avi | ASF | ASX | MPG | MPEG | MP4 | PLS | MP3 | mid | WAV | FLV | TXT | exe | zip | RAR | GZ | tgz | bz2 | uha | 7z | Doc | docx | XLS | XLSX | PDF | ISO) $ # {access_log off; # include/etc/nginx/CONF. d/ipdeny. conf; #} location ~. * \. (PhP | PhP5 )? $ {# Reverse proxy to port 8000 on the local machine. We can go to the site-enabled directory of Apache and find that the site listens to port 8000 on Apache! Proxy_pass http: // 127.0.0.1: 8000; # access_log off; # include/etc/nginx/CONF. d/ipdeny. conf;} # error_page 403 http://whatwhat.xxx/sss.html}

I still don't know much about nginx logs. I hope it will be reflected in the next series of learning articles.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.