赞
踩
sudo yum install -y yum-utils
# 新建文件 vim /etc/yum.repos.d/nginx.repo # 添加文件源 [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true
yum install -y nginx
yum list | grep nginx
whereis nginx
systemctl start nginx
ps -ef | grep nginx
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl reload nginx
systemctl status nginx
systemctl enable nginx
user www-data; #master_process on| off #指定是否开启工作进程 worker_processes auto; #设置工作进程个数 daemon off; #是否以守护进程运行 pid /run/nginx.pid; #用来配置nginx进程id存放位置 include /etc/nginx/modules-enabled/\*.conf; events { worker_connections 768; #单个worker进程最大连接数 multi_accept on; #设置是否允许周时接收多个网络链接 } http { sendfile on; #是否使用sendfile()传输文件,可以大大提高nginx文件传输 tcp_nopush on; # types_hash_max_size 2048; # server_tokens off; # server_names_hash_bucket_size 64; # server_name_in_redirect off; keepalive_timeout 75s #用来设置长链接的超时时间 keepalive_requests 100; #用来设置一个keep-alive连接使用的次数 include /etc/nginx/mime.types; default_type application/octet-stream; #响应浏览器请求默认MIME类型 ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE ssl_prefer_server_ciphers on; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; #配置错误日志 gzip on; }
server { listen 80; server_name 47.107.101.79; index index.html index.htm default.htm default.html; location /get_text { default_type text/html; #返回html类型 return 200 "<h1>this is a text</h1>"; } location /get_json { default_type application/json; #返回json类型 return 200 "{'name':'hello'}"; } }
vim /etc/profile
export PATH=$PATH:/usr/sbin;
source /etc/profile
nginx -v
server
{
listen 80;
server_name www.xiaozhi.shop m.xiaozhi.shop doc.xiaozhi.shop;
...
}
server
{
listen 80;
server_name *.xiaozhi.shop;
...
}
server
{
listen 80;
server_name ~^www\.(\w+)\.shop;
....
}
location /abc {
default_type text/plain;
return 200 "success";
}
location =/abc {
default_type text/plain;
return 200 "success";
}
location ~/abc\w$ {
default_type text/plain;
return 200 "success";
}
location ~*^/abc\w$ {
default_type text/plain;
return 200 "success";
}
location ^~/abcd {
default_type text/plain;
return 200 "abcd success";
}
location /images {
root /mnt/html;
}
location /images {
alias /mnt/html/images;
}
location /images {
root /mnt/html;
index index.html 1.png;
}
server{
error_page 404 https://xiaozhi.shop;
}
server{
error_page 404 500 502 503 504 /50x.html;
location /50x.html{
root /mnt/html;
}
}
sendfile on;
tcp_nopush on;
http {
gzip on; # 是否开启压缩
gzip_vary on; # gip压缩时是否携带头信息
gzip_proxied any; # 对服务端返回结时进行gip压缩
gzip_comp_level 6; # 压缩级别
gzip_disable "Mozilla/*"; # 选择情的开启或开闭gip压缩
gzip_buffers 16 8k; # 缓存空间大小
gzip_http_version 1.1; # 选择压缩最低版本
gzip_min_length 20; # 传输数据的大小
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
}
server{
location ~.*\.(html|js|css|png)$ {
expires 1000;
add_header Cache-Control no-store;
root /mnt/html;
}
}
location /api/v1 {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE;
default_type application/json;
return 200 '{"id":1,"name":"tom"}';
}
location ~.*\.(jpeg|jpg|png)$ {
valid_referers no backed *.xiaozhi.shop;
if ($invalid_referer) {
return 403;
}
root /mnt/html;
}
location /write {
set $name tom;
set $age 18;
default_type application/json;
return 200 '{"name":$name,"age":$age}';
}
location /testif {
default_type text/plain;
set $username 'hello';
if ($username) {
return 200 $username;
}
return 200 'username is empty';
}
location /testbreak{
default_type text/plain;
set $username tom;
if ($args) {
set $username jeray;
break;
set $username rose;
}
add_header username $username;
return 200 $username;
}
location /testreturn {
default_type application/json;
return 200 '{"name":"tom","age":20}';
}
location /rewrite {
rewrite ^/rewrite/url\w*$ https://www.baidu.com;
rewrite ^/rewrite/(test)\w*$ /$1;
rewrite ^/rewrite/(demo)\w*$ /$1;
}
location /test {
default_type text/plain;
return 200 'test success';
}
location /demo {
default_type text/plain;
return 200 'demo success';
}
rewrite_log on;
error_log /var/log/nginx/error.log notice;
location / {
proxy_pass https://8.134.182.122;
}
location / {
proxy_set_header username tom;
proxy_pass https://8.134.182.122;
}
location / {
proxy_redirect http://47.107.101.79/ http://8.134.182.122/;
}
upstream backend {
server 8.134.182.122;
server 106.15.74.79;
}
server
{
listen 80;
server_name 47.107.101.79;
location / {
proxy_pass http://backend;
}
}
upstream backend {
server 8.134.182.122;
server 106.15.74.79 backup;
}
server
{
listen 80;
server_name 47.107.101.79;
location / {
proxy_pass http://backend;
}
}
upstream backend {
server 8.134.182.122 max_fails=3 fail_timeout=15;
server 106.15.74.79;
}
server
{
listen 80;
server_name 47.107.101.79;
location / {
proxy_pass http://backend;
}
}
upstream backend {
server 8.134.182.122 weight=1;
server 106.15.74.79 weight=10;
}
server
{
listen 80;
server_name 47.107.101.79;
location / {
proxy_pass http://backend;
}
}
upstream backend {
ip_hash;
server 8.134.182.122 weight=1;
server 106.15.74.79 weight=10;
}
server
{
listen 80;
server_name 47.107.101.79;
location / {
proxy_pass http://backend;
}
}
upstream backend {
least_conn;
server 8.134.182.122;
server 106.15.74.79;
}
server
{
listen 80;
server_name 47.107.101.79;
location / {
proxy_pass http://backend;
}
}
upstream backend {
hash &request_uri;
server 8.134.182.122;
server 106.15.74.79;
}
server
{
listen 80;
server_name 47.107.101.79;
location / {
proxy_pass http://backend;
}
}
upstream backend { fair; server 8.134.182.122; server 106.15.74.79; } server { listen 80; server_name 47.107.101.79; location / { proxy_pass http://backend; } }
http{
proxy_cache_path /var/log/nginx/cache levels=2:1 keys_zone=xiaozhi:200m inactive=1d max_size=20g;
}
location / {
proxy_cache xiaozhi;
proxy_cache_key xiaozhi;
proxy_cache_valid 200 5d;
proxy_pass http://backend;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。