Articles of 重写

尝试使用Nginx的try_files来模拟Apache MultiViews

我想请求http://example.com/foobar返回http://example.com/foobar.jpg 。 (或.gif,.html,.whatever) 这对Apache MultiView来说是微不足道的,在Nginx中看起来也是一样的。 这个问题似乎意味着它会很容易,因为try_files $uri $uri/ index.php; 在位置块,但这是行不通的。 try_files $uri $uri/ =404; 不起作用,也不try_files $uri =404; 或者try_files $uri.* =404; 在我的location / {块和匹配图像的正则expression式之间移动它没有任何影响。 至关重要的是, try_files $uri.jpg =404; 可以工作,但只适用于.jpg文件,如果在位置块中使用多个try_files规则,则会引发configuration错误! 当前server { block: server { listen 80; server_name example.org www.example.org; access_log /var/log/nginx/vhosts.access.log; root /srv/www/vhosts/example; location / { root /srv/www/vhosts/example; } location ~* \.(?:ico|css|js|gif|jpe?g|es|png)$ { expires max; add_header […]

基于url的stream量redirect到不同的ip地址维护端口和url信息

我们有一个QA版本,一个UAT版本和一个DEV版本的webapp。 用户需要通过http://uat.company.com:41002/webapp和http://dev.company.com:41002/webapp进入。 在端口41001上还有一个不同的webapp,他们也需要访问端口8080。 这些url需要在公司外部可用,我们只有一个公共IP地址可以访问。 因为这样的DNSlogging需要全部3个地址来指向一个IP。 在单个IP地址上,服务器驻留在运行nginx。 在后台我需要每个url指向一个不同的服务器 http://uat.company.com –> 123.123.123.1 http://qa.company.com –> 123.123.123.2 http://dev.company.com –> 123.123.123.3 恐怕我不知道正确的术语,但是URI和端口的其余部分也必须结转到IP地址。 即如果有人访问 http://uat.company.com:41002/webapp/somepage` 它会看起来好像是他们访问的页面,但他们真的会看 http://123.123.123.1:41002/webapp/somepage 或者如果他们访问 http://qa.company.com:8080/static/home.html 他们真的会在看 http://123.123.123.2:8080/static/home.html 但他们的浏览器仍然会说http://qa.company.com:8080/static/home.html 我努力了 server { server_name uat.company.com; listen 41001; listen 41002; listen 8080; location / { proxy_pass http://123.123.123.1:$server_port$uri; proxy_set_header Host $host; } } 但是,这给了我一个坏的网关502页与日志: 2015/01/28 16:04:49 [crit] 30571#0: *1 connect() to […]

使用Apache将http://example.comredirect到http://example.com/mylink的简单方法是?

刚开始尝试了解所有的指令和选项如何在Apache下工作。 我希望对我的一个站点(只在服务器上运行一个站点)进行redirect,这样当http:/example.com发出请求时,服务器会自动将它们redirect到http://的子urlexample.com/mylink 。 我试图把redirect到位于/ etc / apache2 / sites-enabled的文件来重写这个,但是然后顶级域名的URL抱怨它没有正确的redirect。 我想我想要的是浏览器redirect,并认为使用 RewriteEngine On RewriteRule ^/$ /mylink [L,R] 将工作,但将其放入一个.htaccess文件不起作用(redirect,但立即给500内部服务器错误。) 把它放到启动了/ etc / apache2 / sites-enabled的文件中,在尝试重新启动Apache时会出现configuration错误。 我知道这是简单的…但是我错过了什么?

从root .htaccess重写规则中排除一个目录以允许它被密码保护?

上个星期我花了很多时间来解决这个问题,但不pipe我尝试了什么,我都无法把它工作。 我的网站主持人说,他们没有任何人对htaccess文件有足够的了解,以使其运行起来(鼓励,对吧?),而我从CMS购买CMS的公司也无法确定解决scheme。 我也在这里和其他网站上看过几十个答案,但是我根本无法解决我的问题。 这里的交易:我的CMS – Invision电源板 – 生成以下.htaccess文件,它位于网站的根目录: <IfModule mod_rewrite.c> Options -MultiViews RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} .*\.(jpeg|jpg|gif|png)$ RewriteCond %{REQUEST_FILENAME} !-f RewriteRule . /public/404.php [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> 我有一个子目录sub_dir ,我需要在子目录中使用.htaccess文件进行密码保护。 我通过cPanel设置了密码保护,如果网站根目录下的.htaccess文件被禁用,它可以正常工作。 如果网站根目录下的.htaccess文件没有被禁用,那么当我浏览到sub_dir ,主页被显示。 起初,我认为显示的主页可能与404页面重写有关,但即使我删除了404页面的重写条件和规则,主页仍然显示。 sub_dir .htaccess文件中的sub_dir是: AuthUserFile "/home/[user]/public_html/.htpasswd" AuthType Basic AuthName "subdir" require valid-user 正如我之前提到的,密码保护在根目录中的.htaccess文件被禁用时起作用。 它也适用于当我删除底部重写条件和规则(index.php的行),但删除404页的重写条件和规则不影响任何东西。 […]

一起使用nginx的try_files和autoindex

我有以下服务器configuration: server { listen 80; server_name _; root /var/www/; location /calendars/ { autoindex on; try_files $uri.ics $uri =404; } } 如果期望得到访问http://example.com/calendars/的autoindex页面,但是我得到一个404 File not found错误,而不是。 我想让服务器做这样的伪代码: if($uri is directory) { if(one of index pages exists in directory) { show index page; } else { show autoindex page; } } else { if($uri.ics exists) { show $uri.ics; […]

重写规则:追加URL的path

更新 感谢所有迄今为止的答复。 我想清楚地说明,在我的例子中user (例如user1)之后的数字并不重要。 用户可以是任何字母数字名称。 我想确保如果指定了一个目录,我们在它的末尾添加“/faces/main.jsf”。 例如 我们想要URL看起来像: example.com/user1/faces/main.jsf 该系统可以附加像(我们必须允许的) http://example.com/user1/faces/main.jsf;jsessionid=Z_DV-bY6MuQGlIvflPwgdKDk60cNzOkWXbC5G18ILrjR0jKoGWnd!-617980607 如果请求看起来像这样: example.com/user1 example.com/user1/ 我想要“/faces/main.jsf”被追加到它。 我的重写规则有什么问题? <VirtualHost *:80> ServerName example.com ProxyPass / http://1.example.com:8888/ ProxyPassReverse / http://1.example.com:8888/ RewriteEngine On # RewriteLog /tmp/rewrite.log RewriteCond %{HTTP_HOST} !^example.com$ RewriteRule !^/(.) http://example.com/$1 [L,R] RewriteCond %{REQUEST_URI} !^/(.+/)faces/main.jsf.* RewriteRule !^(.+)/faces/main.jsf.*$ $1/faces/main.jsf [L,R] </VirtualHost>

nginxredirect到另一个不忘记子域名的域名

这是一个相当快的一个:我如何redirect到另一个域,仍然转发子域? 例如: http : //foo.domainone.com/bar.php – > http://foo.domaintwo.com/bar.php 提前致谢!

Nginx重写,以消除“www”不工作

我已经看了许多SE线程,并做了各种谷歌search ,不知道为什么我不能将www.mysite.comredirect到我的nginx服务器上的mysite.com 。 第一个服务器块按照你的预期做了http://mysite.info – > https://mysite.inforedirect。 所以我不知道为什么第二个服务器块不会为www.mysite.info – > mysite.info做同样的mysite.info 。 这里是我的nginx.conf文件的相关部分: server { server_name mysite.info; rewrite ^ https://$server_name$request_uri? permanent; } server { server_name www.mysite.info; rewrite ^ https://mysite.info$request_uri? permanent; } server { listen 443; ssl on; server_name mysite.info; # other directives, handling PHP, etc. } 有什么想错了?

nginx只将特定的服务器名称重写为https

这是关于两个子域。 第一个(www)应该通过http访问。 第二个(云)应该通过https访问。 这些是我的作品的相关部分: server { listen 80; server_name cloud.example.de; rewrite ^ https://$server_name$request_uri? permanent; # enforce https } server { listen 443 ssl; server_name cloud.example.de; root /home/user/web/cloud; } server { listen 80; server_name www.example.de; root /home/user/web/cms; #etc. } 当我现在打电话http://cloud.example.de我redirect到https://cloud.example.de ,很好。 但是当我打电话给http://www.example.de时,我也被redirect到https://www.example.de ,这导致了我的内容cloud.example.com,因为这是唯一的服务器名设置为由端口443使用。 www-subdomain的访问日志中没有条目。 还有一个子域指向一个phpPgAdmin。 这我可以正常访问,它不会被重写。 server { listen 80; server_name pgsql.example.de; root /home/user/web/phppgadmin; #etc } […]

Nginx用一个外部程序重写

在Apache的mod_rewrite模块中有一个叫做RewriteMap的特性。 这允许调用外部程序来处理redirect(允许更复杂的redirect)。 Nginx是否有相同的function? 我已经看了HttpMap和重写模块,但无法find一种方法来做到这一点。 任何指针都会很棒。