首页 > 系统相关 >Nginx安装与各类配置集合

Nginx安装与各类配置集合

时间:2022-10-17 20:05:48浏览次数:58  
标签:127.0 http nginx 0.1 Nginx proxy 集合 安装 location

安装

vi /etc/yum.repos.d/nginx.repo
#Stable version
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

#Mainline version
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=0

禁止所有未授权域名及IP访问

添加一条规则如下  ,加在最后一个server后面

server {
listen 80 default;
server_name _;
return 403;
}

将未授权指向的域名跳转的指定页面

将 return 403;(返回403错误提示)修改为;

rewrite ^(.*) http://www.v1lady.com/stop.htmlpermanent;

禁止未授权域名访问 允许服务器IP的访问

server

{
listen 80;
server_name v1lady.com 42.121.31.169;
index index.html index.htm index.php;
root /home/wwwroot;
}



最后重启一下nginx

/usr/local/nginx/sbin/nginx -s reload

Nginx反向代理'/'的作用

第一种:

location /proxy/ {

proxy_pass http://127.0.0.1:81/;

}

会被代理到http://127.0.0.1:81/test.html 这个url



第二种(相对于第一种,最后少一个 /)

location /proxy/ {

proxy_pass http://127.0.0.1:81;

}

会被代理到http://127.0.0.1:81/proxy/test.html 这个url



第三种:

location /proxy/ {

proxy_pass http://127.0.0.1:81/lxy/;

}

会被代理到http://127.0.0.1:81/lxy/test.html 这个url。



第四种情况(相对于第三种,最后少一个 / ):

location /proxy/ {

proxy_pass http://127.0.0.1:81/lxy;

}

会被代理到http://127.0.0.1:81/lxytest.html 这个url

从结果可以看出,应该说分为两种情况才正确。即http://127.0.0.1:81 (上面的第二种) 这种和http://127.0.0.1:81/.... (上面的第1,3,4种) 这种。

取消nginx上传目录php执行权限

将以下代码添加至Server容器中的合适位置,也就是在定义fastcgi的规则之前.
location ~* ^/upload/.*\.(php|php5)$
{
deny all;
}

用Nginx做代理后PHP取不到HTTP_X_FORWARDED_FOR值

用nginx做前端代理后,我们发现 HTTP_X_FORWARDED_FOR 无法获取到客户端真实的IP地址了。
原因 nginx 默认并不会增加 X_FORWARDED_FOR 头信息,我们给他加上就好了。简单配置如下:
location /
{
proxy_pass http://www.92csz.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; //别忘了这一句
proxy_set_header HTTP_X_FORWARDED_FOR $remote_addr; //或是加上这一句
proxy_redirect default;
}
重启nginx加载新配置后,就可以获取客户端真实的IP地址了。

nginx php配置+反向代理配置

   server {
listen 80;
server_name localhost;
location / {
root /usr/local/nginx/billing;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root share/examples/nginx/html;
}

location ^~ /test { //location 的 ^~ 含义是匹配此路径后无视其他 location 配置, 包括 location /。这样 rewrite 或其他就配置不会对此产生任何影响。
proxy_pass http://183.62.9.66:50080/;
}


location ~ \.php$ {
root /usr/local/nginx/billing;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #$document_root是location ~ \.php$中root的值
include /opt/local/etc/nginx/fastcgi_params;
}

}

www跳转到不带www

server {
server_name www.starsl.cn;
return 301 $scheme://starsl.cn$request_uri;
}

http跳转https

server
{
listen 80;
listen 443 ssl;
server_name www.starsl.cn;
if ($scheme = 'http') { return 301 https://$host$request_uri; }
}


标签:127.0,http,nginx,0.1,Nginx,proxy,集合,安装,location
From: https://blog.51cto.com/starsliao/5764136

相关文章