The Midrash (Bamidbar Rabbah 14:6) expounds the Pesukim in Koheles (8:2-3):
Based on several incidents, the Midrash interprets "ani" as indicating that one's awe should be over you. Thus, "Ani, the king's command, obey" means "obey the king's command to fear him." I.e. don't rebel against him.
You might think this is true even if he says to disobey the Torah; therefore the passuk continues, "and above, the word of an oath of G-d." Just like one may not obey his parents if they tell him to disobey the Torah (specifically Shabbos is the example brought), so may one not obey a king if he tells him to disobey the Torah. Fine.
The Midrash continues: "Do not become flustered by his countenance and go" - even if he becomes angry with you, do not sin. It brings a proof that "countenance" indicates anger from a passuk in Daniel and that "going" means sinning from a passuk in Tehillim. "Do not stand because of an evil thing" - do not sin because of his threats. In the Midrash's wording, "that you should not be scared by his evil thing he says to you, that he'll burn you, that he'll kill you, that he'll torture you if you don't listen to him."
Yet, the Gemara (Sanhedrin 74a) says that, typically speaking, one does not have to give up his life to fulfill mitzvos. So how can the Midrash say that one should not listen to the king's decree even on pain of death? From the comparison to parents ordering their children to disobey Shabbos, it sounds like that is the case of the king as well - ordering his subjects to disobey the "lighter" mitzvos. How can this be, when we have a rule that one shouldn't give up his life in such circumstances?
Granted, the above Gemara also says that in a time of persecution (read: they want you to denounce Judaism), one should be killed even for a well-rooted minhag such as the order of tying one's shoes (see Rashi there). The discussion of a king being upset with his subject would seem to indicate that he simply wants him to violate the Torah, not that he wants him to give up Yiddishkeit. Must we deduce that a king's order is comparable to a time of persecution, and if so, why?