Can rewrite change port? for eg, 80 to 443

Hi, everyone.

I have a server running a php web forum. User logging into my site
using ‘logging.php’.

I had set up a https server using nginx, but consdering server load, I
just want my user using https in only logging.php.

I want to settle this by using url rewrite. When my users click
http://mysite.com/logging.php, nginx will automatic change the url to
https://mysite.com/logging.php. And when my user click any other links
in my site, for example, https://mysite.com/index.php, nginx will
change https to http.

Can rewrite work like this?

Thank you!

BTW,changing my forum codes may work, but I didn’t know much about php
coding…so, I want to settle this in nginx rewrite…

In your plain http server block:

if ($uri ~* “/logging.php$”) {
rewrite ^/(.*)$ https://$host/$1 redirect;
}

In your https server block

if ($uri !~* “/logging.php$”) {
rewrite ^/(.*)$ http://$host/$1 redirect;
}

This is when you are using standard ports (80 for HTTP and 443 for
HTTPS). If you are using non-standard ports (say 8080 for http, and 8443
for https, then in this case, you should have in your http block)

if ($uri ~* “/logging.php$”) {
rewrite ^/(.*)$ https://:8443$host/$1 redirect;
}

and correspondingly, in your https block, you should have:

if ($uri !~* “/logging.php$”) {
rewrite ^/(.*)$ http://:8080$host/$1 redirect;
}

The $host variable is the host portion of the URL that was used to reach
your server
See http://wiki.codemongers.com/NginxHttpCoreModule for the list of
variables

HTH
Mansoor

----- “baalchina” [email protected] wrote:

| From: “baalchina” [email protected]
| To: [email protected]
| Sent: Monday, July 21, 2008 12:48:36 PM GMT +05:30 Chennai, Kolkata, Mumbai, New Delhi
| Subject: Can rewrite change port? for eg, 80 to 443
|
| Hi, everyone.
|
| I have a server running a php web forum. User logging into my site
| using ‘logging.php’.
|
| I had set up a https server using nginx, but consdering server load,
| I
| just want my user using https in only logging.php.
|
| I want to settle this by using url rewrite. When my users click
| http://mysite.com/logging.php, nginx will automatic change the url to
| https://mysite.com/logging.php. And when my user click any other
| links
| in my site, for example, https://mysite.com/index.php, nginx will
| change https to http.
|
| Can rewrite work like this?
|
| Thank you!
|
| BTW,changing my forum codes may work, but I didn’t know much about
| php
| coding…so, I want to settle this in nginx rewrite…
|
| –
| from:baalchina

Thanks mansoor!

It works perfect!

2008/7/21, Mansoor P. [email protected]:

Thanks Mansoor,

This info is very handy to know about.

On Mon, Jul 21, 2008 at 01:40:09AM -0700, Mansoor P. wrote:

rewrite ^/(.*)$ http://$host/$1 redirect;

if ($uri !~* “/logging.php$”) {
rewrite ^/(.*)$ http://:8080$host/$1 redirect;
}

The $host variable is the host portion of the URL that was used to reach your server
See http://wiki.codemongers.com/NginxHttpCoreModule for the list of variables

“if ($uri …)” expressions are good candidates for locations:

plain server

location = /logging.php$ {
rewrite ^/(.*)$ https://$host/$1 redirect;
}

https server

location / {
rewrite ^/(.*)$ http://$host/$1 redirect;
}

location = /logging.php$ {

}

Yes, that’s right. Always better to have the rule inside a location
block, rather than the main conf.
Just as a matter of interest, do we have .htaccess support in NGINX ?

----- “Maxim D.” [email protected] wrote:

| From: “Maxim D.” [email protected]
| To: [email protected]
| Sent: Monday, July 21, 2008 4:25:09 PM GMT +05:30 Chennai, Kolkata, Mumbai, New Delhi
| Subject: Re: Can rewrite change port? for eg, 80 to 443
|
| Hello!
|
| On Mon, Jul 21, 2008 at 02:10:07PM +0400, Igor S. wrote:
|
| >On Mon, Jul 21, 2008 at 01:40:09AM -0700, Mansoor P. wrote:
| >
| >> In your plain http server block:
| >>
| >> if ($uri ~* “/logging.php$”) {
| >> rewrite ^/(.)$ https://$host/$1 redirect;
| >> }
| >>
| >>
| >> In your https server block
| >>
| >> if ($uri !~
“/logging.php$”) {
| >> rewrite ^/(.)$ http://$host/$1 redirect;
| >> }
| >>
| >>
| >> This is when you are using standard ports (80 for HTTP and 443 for
| HTTPS). If you are using non-standard ports (say 8080 for http, and
| 8443 for https, then in this case, you should have in your http
| block)
| >>
| >> if ($uri ~
“/logging.php$”) {
| >> rewrite ^/(.)$ https://:8443$host/$1 redirect;
| >> }
| >>
| >> and correspondingly, in your https block, you should have:
| >>
| >> if ($uri !~
“/logging.php$”) {
| >> rewrite ^/(.)$ http://:8080$host/$1 redirect;
| >> }
| >>
| >> The $host variable is the host portion of the URL that was used to
| reach your server
| >> See http://wiki.codemongers.com/NginxHttpCoreModule for the list of
| variables
| >
| >“if ($uri …)” expressions are good candidates for locations:
| >
| > # plain server
| > location = /logging.php$ {
|
| - location = /logging.php$ {
| + location = /logging.php {
|
| > rewrite ^/(.
)$ https://$host/$1 redirect;
| > }
| >
| > # https server
| >
| > location / {
| > rewrite ^/(.*)$ http://$host/$1 redirect;
| > }
| >
| > location = /logging.php$ {
|
| - location = /logging.php$ {
| + location = /logging.php {
|
| > …
| > }
|
| Maxim D.

Hello!

On Mon, Jul 21, 2008 at 02:10:07PM +0400, Igor S. wrote:

plain server

location = /logging.php$ {

  • location = /logging.php$ {
  • location = /logging.php {
  rewrite ^/(.*)$ https://$host/$1 redirect;

}

https server

location / {
rewrite ^/(.*)$ http://$host/$1 redirect;
}

location = /logging.php$ {

  • location = /logging.php$ {
  • location = /logging.php {
  ...

}

Maxim D.

Hello!

On Mon, Jul 21, 2008 at 05:24:35AM -0700, Mansoor P. wrote:

Yes, that’s right. Always better to have the rule inside a location block, rather than the main conf.
Just as a matter of interest, do we have .htaccess support in NGINX ?

No.

Maxim D.

On Mon, Jul 21, 2008 at 05:24:35AM -0700, Mansoor P. wrote:

Yes, that’s right. Always better to have the rule inside a location block, rather than the main conf.
Just as a matter of interest, do we have .htaccess support in NGINX ?

No, I do not plan to support .htaccess-like configurations.