天天看點

Nginx 基礎知識必讀

一,Linux 6,7 下的安裝依賴

yum -y install pcre-devel openssl openssl-devel libssl-dev gcc-c++

二,安裝 pcre,讓 Nginx 支援 Rewrite 功能

0,pcre 位址:

https://sourceforge.net/projects/pcre/

1,下載下傳:wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.44.tar.gz

2,解壓:tar zxvf pcre-8.44.tar.gz

3,進入:cd pcre-8.44

4,配置:./configure

5,編譯:make

6,安裝:make install

7,驗證:pcre-config --version,會出現:8.44,表示安裝成功!

三,安裝 Nginx

1,下載下傳:wget

http://nginx.org/download/nginx-1.16.1.tar.gz

2,解壓:tar zxvf nginx-1.16.1.tar.gz

3,進入:cd nginx-1.16.1

4,配置:./configure(可以加上:--with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.44)

7,驗證:/usr/local/nginx/sbin/nginx -t,出現:nginx: ... syntax is ok ... test is successful ,表示安裝成功!

四,CentOS 7 安裝 Nginx

1,添加 yum 源:rpm -ivh

http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

2,安裝:yum -y install nginx

3,啟動:systemctl start nginx

4,開機自啟動:systemctl enable nginx

5,重新開機服務:systemctl restart nginx

6,重新加載配置:systemctl reload nginx

7,配置檔案:配置檔案:/etc/nginx/nginx.conf

8,預設配置檔案:/etc/nginx/conf.d/default.conf

五,配置

1,啟動服務:/usr/local/nginx/sbin/nginx

2,關閉服務:/usr/local/nginx/sbin/nginx -s stop

3,重新開機服務:/usr/local/nginx/sbin/nginx -s reload

4,加入系統服務:vim /etc/rc.local,添加:/usr/local/nginx/sbin/nginx

5,在 /usr/local/nginx/conf 中建立:gzip.conf

gzip on;

gzip_min_length 1k;

gzip_buffers 4 16k;

gzip_types text/plain text/css application/x-javascript;

output_buffers 1 64k;

postpone_output 1460;

6,在 /usr/local/nginx/conf 中建立:proxy.conf

proxy_redirect off;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

client_max_body_size 10m; #檔案上傳大小限制

client_body_buffer_size 256k;

proxy_connect_timeout 300;

proxy_send_timeout 300;

proxy_read_timeout 300;

proxy_buffer_size 16k;

proxy_buffers 4 32k;

proxy_busy_buffers_size 64k;

proxy_temp_file_write_size 64k;

六,最精簡的 nginx.conf

Nginx 基礎知識必讀

七,Nginx 負載均衡配置:

upstream test {

server localhost:8080;

server localhost:8081;

}

server {

listen 80;

server_name localhost;

client_max_body_size 1024M;

location / {

    proxy_pass http://test;

    proxy_set_header Host $host:$server_port;

繼續閱讀