nginx常用配置

|▌冷眼眸甩不掉的悲伤 提交于 2020-05-07 18:43:26

Nginx常用配置

vhosts.conf(或者写在http内)

代理80端口

server
{
    listen 80;
    server_name fastphp.vm;
    index index.php;
    root /www/fastphp/public/;
        location / {
            index index.php;
            if (!-e $request_filename) {
                rewrite ^/(.*)$ /index.php/$1 last;
                break;
            }
            try_files $uri $uri/ =404;
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   php:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}

反向代理80端口

#docker反向代理
server
{
    listen 80;
    server_name easyswoole.vm;
    location / {
        #这里php指的是docker的container_name,也可以代理到其它ip
        proxy_pass http://php:9501;
        proxy_redirect default;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
#一般的反向代理
server
{
    listen 80;
    server_name redis.local;
    location / {
        proxy_pass http://redis.vm;
        proxy_set_header Host $proxy_host;
        proxy_redirect default; 
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
#proxy_set_header Host  $host;请求redis.vm时,设置请求头信息Host为redis.vm
#proxy_set_header Host  $proxy_host;  请求redis.local时,设置请求头信息Host为redis.vm
#因为nginx是根据server_name判断的,所以如果头信息Host设置不正确,可能就会达不到想要的代理效果

nginx.conf

反向代理其它端口(如3306)

stream {
    upstream TCP3306 {
        hash $remote_addr consistent;
        # least-connected;
        # least_time first_byte;
        server your_proxy_ip:3306 max_fails=3 fail_timeout=30s;
    }
    server {
        listen 3306;
        proxy_connect_timeout 30s;
        proxy_timeout 600s;
        proxy_pass TCP3306;
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!