sudo apt-get install nginx
默认安装位置:
/usr/sbin/nginx:主程序
/etc/nginx:存放配置文件
/usr/share/nginx:存放静态文件
/var/log/nginx:存放日志
指令
启动nginx
停止nginx -s stop
重载nginx -s reload
配置文件
vi /etc/nginx/nginx.conf
vi /etc/nginx/sites-enabled/default
worker_processes auto; #CPU核心数
worker_rlimit_nofile 65535; # ulimit -n
events {
multi_accept on;
use epoll;
worker_connections 1024;
}
http {
sendfile on; #默认就是on的
aio on;
directio 4m;
tcp_nopush on;
tcp_nodelay on;
}
server {
listen 80; # 监听端口
server_name www.xuehu96.com; #监听域名/主机名/IP
location / {
root html; #/usr/share/nginx/html;
index index.html index.htm;
}
location ~/myweb { #区分大小写,即访问Myweb是不行的
root html/localtion; #定义myweb所在的路径,即在浏览器访问myweb的时候,实际是访问的html/localtion/myweb目录里面的web内容
index index.html; #默认首页文件类型
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
虚拟主机(多IP、多port、多host域名)
server_name localhost www.a.com; #多个域名用空格间隔即可
server_name 192.168.0.2; #IP是本机的网卡IP地址
多域名:
server {
listen 80;
server_name www.abc.com;
location / {
proxy_pass http://192.168.1.10;
}
}
server {
listen 80;
server_name image.test.com;
location / {
alias /var/www/image;
}
}
server {
listen 80;
server_name video.abc.com;
location / {
proxy_pass http://192.168.1.10:8081/video;
}
}
在上述配置中,将三个域名分发给了不同的模块处理:
/ 通用匹配 相当于default
= #用于标准uri前,需要请求字串与uri完全匹配,如果匹配成功就停止向下匹配并立即处理请求。
~ #区分大小写
~* #不区分大写
!~ #区分大小写不匹配
!~* #不区分大小写不匹配
^ #匹配以什么开头
$ #匹配以什么结尾
\ #转义字符。可以转. * ?等
* #代表任意长度的任意字符
-f和!-f #用来判断是否存在文件
-d和!-d #用来判断是否存在目录
-e和!-e #用来判断是否存在文件或目录
-x和!-x #用来判断文件是否可执行
列出目录
location /images {
root /var/www/nginx-default/images;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
静态资源直接处理
location ~ \.(css|js|fonts|png|svg|html|txt)$ {
#access_log on;
expires 1d; #过期时间
root /newhome/go/gowork/goonlinemusic/static;
try_files $uri $uri/;
}
不同浏览器
upstream android {
server 172.16.1.7:9090;
}
upstream iphone {
server 172.16.1.7:9091;
}
upstream pc {
server 172.16.1.7:9092;
}
server {
listen 80;
server_name sj.drz.com;
charset 'utf-8';
location / {
#如果客户端来源是Android则跳转到Android的资源;
if ($http_user_agent ~* "Android") {
proxy_pass http://android;
}
#如果客户端来源是Iphone则跳转到Iphone的资源;
if ($http_user_agent ~* "Iphone") {
proxy_pass http://iphone;
}
#如果客户端是IE浏览器则返回403错误;
if ($http_user_agent ~* "MSIE") {
return 403;
}
#默认跳转pc资源;
proxy_pass http://pc;
}
}
setheader
server {
listen 80;
server_name www.xxx.com;
location /xxx/
{
proxy_pass http://192.168.1.10:8080/xxx/;
proxy_set_header host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
go server
## 设置反向代理,指向go服务器 ##
server {
listen 8081;
location / {
proxy_pass http://localhost:9090;
#go 服务器可以指定到其他的机器上,这里设定本机服务器
#Proxy Settings
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;
}
}
## End 设置反向代理,指向go服务器 ##
##
# Upstream Settings
##
upstream test {
server 127.0.0.1:777 weight=5;
server 127.0.0.1:5000 weight=5;
server 192.168.12.100:5000 fail_timeout=2s max_fails=1;
}
server {
listen 8081;
location / {
proxy_pass http://test;
#Proxy Settings
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;
}
}
fail_timeout 设置的时间内服务器没有响应则认为服务器失效,默认10s
max_fails 允许连接失败的次数,默认1
fail_time 服务器被移除轮询队列的时间,默认10s(在移除的时间内不再被轮询请求)
backup 标记该服务器为备用服务器,当主服务器宕机时,它将会代替主服务器接收请求
down 标记此服务器永久停机
server {
listen 443 ssl;
server_name ssl.xuehu96.com; #绑定证书的域名
ssl_certificate 1_ssl.xuehu96.com_bundle.crt;#绝对路径
ssl_certificate_key 2_ssl.xuehu96.com.key; #改为绝对路径
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://127.0.0.1:8886;
}
}
安装
sudo apt-get install php7.2 php7.2-fpm php-json php-curl php7.2-mysql php7.2-cgi
vi /etc/nginx/sites-enabled/default
server {
listen 80 default_server;
listen [::]:80 default_server;
root /root/www/default;
index index.php index.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
# pass PHP scripts to FastCGI server
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets):
#fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# With php-cgi (or other tcp sockets):
fastcgi_pass 127.0.0.1:9000;
}
}
vi /etc/php/7.2/fpm/pool.d/www.conf
36 listen = 127.0.0.1:9000
service nginx restart
service php7.2-fpm restart